# Using Formula Functions

Objectives

After completing this lesson, you will be able to:

• Describe formular foundations and formula editor.
• Calculate with formula operators.
• Use formula variables and functions.

## Formula Editor

SAP Build Apps has a Formula Editor that is one of the most powerful algorithm editors you will find. But don’t worry – even though the word algorithm makes it sound complicated, that has been simplified for you to use. SAP Build Apps has combined "spreadsheet type" formulas with built-in support for a full application context that can help you create a complex algorithm in a simple way.

You can combine hundreds of data transformation functions with relevant data such as GPS location, sensor values, data properties, and so on.

Play the video to learn how to use Formula Editor.

## Formula Operators

Even though SAP Build Apps does not require any code, the knowledge of operators is also indispensable here and helps a lot in designing dynamic apps.

Arithmetic operators
Arithmetic operators are symbols that indicate a mathematical operation to be performed on one or more operands (values). The most common arithmetic operators are addition (+), subtraction (-), multiplication (*) and division (/). Other arithmetic operators include modulus (%) and exponentiation (^).

In a simple example, a variable for a coffee or water counter that starts every day with the value 0 or other stored value from the past can always be incremented by +1 when a function with this addition (+1) operator is triggered.

It should be noted that the addition operator is additionally used, for example, to link other strings or variables together to create dynamic content.

Logical operators

Logical operators ||, && and ! are used to compare two values or expressions. The logical OR (||) operator returns true if either of the operands is true. The logical AND (&&) operator returns true only if both operands are true. The logical NOT (!) operator reverses the result of an expression, returning false if the expression is originally evaluated as true and vice versa.

A common example of the AND operator is when a user needs to provide both the correct email and password in a login form in order to gain access. The OR operator (||) could be used as in the exercise app when planning a dinner meal. Then if a participant is either vegan or has allergies, they could receive an alternative meal plan as a result.

Comparisons

Comparison operators like ==, ===, != and !== are used to compare two values. The double equality operator (==) checks if the values are equal, while the triple equality operator (===) additionally compares the type. The inequality operators (!= and !==) check if the values are unequal or not identical. The <, <=, >, and >= operators compare whether a value is less than, less than-equal to, greater than, or greater than-equal to another value.

Examples of use can be to check whether a person has reached a certain age for an event (for example: 18; var_user>= 18) or whose name matches the check name (var_name == John). Useful here is also that it can be used for a counter with a certain target value, whereby functions repeat in a loop until a target value is reached. Depending on the initial resulting value (true, false), another action can then take place.

Conditionals

<condition> ? <true value> : <false value>

This shows how we can use these returned values of the operators to subsequently return a different value depending on the output. Conditional statements are a type of statement in programming that allows the programmer to specify two different paths for the program to take, depending on whether a condition is true or false. The syntax for conditionals is "?" If the condition is true, the expression returns the true value, if it is false, it returns the false value.

For example: (x > 5) ? "x is greater than 5": "x is not greater than 5". In this example, if x is greater than 5, "x is greater than 5" is returned; otherwise, "x is not greater than 5" is returned.

Remainders
Remainders are the numbers that remain after a division operation (%). For example, if you divide 7 by 3, the remainder is 1. Remainders can be used to determine whether a number is divisible by another number without having to do any calculations. For example, if you want to know if a number is divisible by 5, you can check the remainder of dividing it by 5 - if it's 0 then it's divisible; otherwise, it isn't.

## Formula Variables

Using formulas, you can access all existing variables in the application that can then be modified.

However, it should be noted that these can only be read, but no new variables can be set or created using the form editor alone. Thus, it becomes clear that, in particular, the common use of flow functions is meaningful.

Besides many already treated variables, here still sensor variables become important, with which the value of device sensors is passed on like -for example- accelerometer variables or system variables like the currently used browser by Logic Flows.

## Formula Functions

In addition to handling operators, the extensive catalog of Form Functions offers the advantage of ready-made functions that can be used in a various ways and quickly. As mentioned before, it is worth going through them in the Formula Editor and reading the descriptions.

Let's look into some frequently used categories.

Text Section

The text section of the Formula Functions allows a wide range of functions to edit, replace, format and also analyze or search texts in the app. Thus, simple to more complex text manipulation is possible.

The simple example with the function UpperCase() converts a given text in such a way that all letters are written in capital letters to emphasize it.

Date

The functions from the Date group are useful to use and work with date and time information. This allows the display of different second values and added to suit the situation, a process or the user (for example: local time zone). Also multiple time values can be related to determine the exact duration between them or simply to determine if a time period is before or after a set time value or identical to it.

In the example shown, the NOW function returns a value corresponding to the current time at which it was triggered and can alternatively be modified to show only the day.

List

With the variety of List components and uses, List functions are also important for displaying content in a dynamic app. Using the functions of this group, lists can be easily sorted and filtered. In addition, they can also be edited, for example, by splitting them or deleting individual elements. Especially for the latter, functions for search and analysis are useful to determine the position or number of items in the list.

In the Count function example, the items of a given list are counted and returned as a numeric value. This can then be passed in the example of a participant list for further calculations to determine the budget or the number of remaining free seats.

Mathematical Functions

The mathematical functions supplement the normal operators with a variety of further predefined possibilities to perform mathematical calculations within the application. Thus values can be compared, the smallest or largest value can be returned or the numbers in an array can be summed. One of the many but frequently used examples are the rounding up (round()) and rounding down (floor()) of values to a whole number or the calculation of the remainder after a division (modulo()).

In the shown example an integer is returned at random, which is generated between two selected integers - including this one. So for this in the example of the participant list, the current number of the participants (42 in this example for pageVars.participants_number) can be used as Max parameter, so that with the randomly generated number a participant is drawn between 0 and 4., Note: Since an array also selects the first item in SAP Build Apps with the 0, the max number (42) was subtracted by 1 in the example.

Utility

Basically, the utility functions are especially used to check conditions and value inputs for the condition general or in many cases here the condition regarding the right format. The focus is on the IF function, which evaluates the input parameter as a condition as true or false and returns a specific value based on this, thus enabling dynamic processes.

In the example, the IS_EMAIL function is used to check whether the given input corresponds to the format of a valid email address. If this is the case, true is returned. On the other hand false. These outputs can then be used as triggers for further actions, such as a popup with the request that the input of the mail is incorrect and should be checked again.