Global formulas We often need for our application to make calculations that involve the values of certain attributes, constants and/or functions. For such cases, GeneXus provides us with its Formulas Page1
We have 2 possible ways to define formulas: Global formulas known in the whole Knowledge Base, And Local formulas, known only in the object in which they were defined. Let s start by learning what a Global formula is and how to define it. Page2
A global formula is a calculation we define in association with an attribute. We can see that the structures of transactions include a column under the title Formula. When we define a calculation for an attribute in this column, Page3
we are telling GeneXus that the attribute is virtual, meaning that it must not be created physically as a field in the associated table because the value of the attribute will be obtained by doing the calculation we indicate. Let s exemplify this: We will define a new attribute in the Flight transaction, to store the current discount applicable to each flight. We enter the attribute called FlightDiscountPercentage where its data type will be of a Percentage domain we define now, as a numeric with length 3. And we define another attribute, called FlightFinalPrice, which we will define as a formula to automatically calculate the current flight price in this attribute. Page4
In the formula column, we define the calculation: FlightPrice * (1 FlightDiscountPercentage/100) And press F5 to see what happens. We can see that only one field is created in the Flight physical table, even when we defined 2 new attributes in the structure of the Flight transaction. Page5
Because it has a definition in the formula column, this attribute is not added to the physical table. Because the attribute is defined in the Knowledge Base with an associated formula, GeneXus will know how to calculate its value, and the calculation will take place in all objects in which the attribute is present, with the result shown. We no reorganize Page6
The programs have been generated and here we see the application in runtime We execute the Flight transaction, and query Flight #1 where on the transaction form we can see the 2 new attributes we defined: Page7
the discount percentage, enabled for us to enter it, and the final price, disabled because it is the attribute we defined as formula, and its value is not entered but rather calculated and then displayed. Every attribute defined as Global formula will be reading data and we will not be able to type in any value for it. This is because the attribute gets its value from the associated calculation, which takes place every time that the attribute is used. Also, there is no field in the physical table to store the value of this attribute, so making it editable would make no sense. So we enter a 10 % discount for this flight. Page8
When we exit the field we can see that the formula is immediately executed Showing the flight s final price with the discount applied. Now let s get back to GeneXus. So far we have defined a Global formula attribute. Page9
Only attributes may be defined as Global formulas in the way we just described and using the formula column in the transaction. Something important to bear in mind is that even when in the example calculation we included only attributes of the transaction itself, it is possible to also include attributes of the extended table. Let s see that We create a transaction called Airline to record the airlines. We define 3 attributes for it: AirlineId, AirlineName, and AirlineDiscountPercentage, from the Percentage domain, to record the discount made by the airline for all its flights. We save, and now go to the Flight transaction, to assign to each flight the airline that operates it. Page10
We add the attribute AirlineId, which here will play the role of foreign key and change the value of its Nullable property to Yes. This allows us to not specify, in this first stage, the flight s airline, because we still don t have any airlines recorded. Further ahead we will come back to change the value of the Nullable property to No, to make specification of the airline mandatory when entering or modifying a flight. We add the attributes AirlineName and AirlineDiscountPercentage to view this data on the form: Page11
We now modify the definition of our formula, so that it calculates the flight s final price, applying to it the airline s generic discount instead of applying the flight s own discount. Then we press F5 Page12
We now see the proposal for creation of the Airline physical table, with the 3 attributes we defined, and for the foreign key AirlineId to be added to the Flight table. Let s now reorganize and execute Page13
We open the Airline transaction and add an airline called TAM, with a 30% discount: Now to this airline we will assign a flight. To flight N 1 we assign airline 1 Page14
and we will see that the flight s new final price has been calculated, as a Global formula attribute Page15
that includes the airline s discount percentage, which is an attribute of the extended table of the Flight base table. Something we haven t said yet is that formulas can assess conditions and the result may be calculated in different ways depending on whether such conditions are true or false. Let s see that: Page16
We click on this button with 3 dots to edit the formula more easily. We will define that the highest discount percentage be considered to calculate the flight s final price, so we get the best possible discount. Page17
With this definition, if the airline has a higher discount for all its flights than the flight s own discount percentage, for the calculation we will consider the airline discount. Otherwise, we use the flight s own discount percentage for the calculation. We then press F5. We execute the Flight transaction and to the first flight we will assign that its own discount percentage be greater than the airline s overall discount percentage, for example: 50%. Page18
We will see that the flight s final price was calculated upon considering the greater discount. Now let s get back to GeneXus. Page19
Formulas may contain several lines followed by IF and they might or might not contain a last line with the OTHERWISE Page20
for the case where none of the previous conditions apply. Also, even when in this example the way to obtain each result was through a calculation, we could also resort to functions applied to attributes or calculations such as Round to obtain a rounded result, or Month to get the month from a date, Page21
among other options... and we can also call a procedure we have defined, to return a value. We will now see another type of formulas which we will define also as global. We will create a second level in the Flight transaction and call it Seat. As the level s name indicates, we will use it to record the seats available on the flight and we will record, for each seat, whether it is window, aisle or middle. Then we will want to know, in all cases, the number of seats available on the flight. We type a period and fill in the attribute name: FlightSeatId Page22
We create another attribute called FlightSeat Location, and assign to it a Location domain of the character(1) type. We now go to the Domains node to modify a property in the domain we have just created. We find the Enum Values property Page23
and define the 3 values that the domain may have: Page24
Window and the value that will be stored when Window is selected will be the character W Middle where the value stored will be an M and Aisle where the value stored will be an A we then press OK, and save the definitions we made in the Flight transaction. Let s take a look at the form in this transaction: Page25
We can see that a grid was added to enter the flight seats, and for each seat, an indication can be made of its location through a combo control that provides the window middle or aisle values, which are possible values we defined for the domain of the FlightSeatLocation attribute. Prior to pressing F5, let s see something in the definition of the second level: Page26
If the key consists of FlightId plus FlightSeatId, for each flight we will not be able to repeat seat numbers. But we need the possibility of repeated numbers, plus a variable letter, since, in general seats are identified as 1A, 1B, etc. So we add a FlightSeatChar attribute, whose type we define as a SeatChar domain, character(1) and make the attribute part of the key Page27
to enable the record of equal seat numbers, with a different letter. We will restrict the possible letters from A to F, and for that we edit the domain. We find its Enum Values property and define the possible values: Page28
In this case, the values of descriptions match the values stored. Now, in order to know the number of passengers allowed on the flight, we will define a new attribute in the first level, as a formula, which will have the number of seats provided by the flight. We create the FlightCapacity, attribute, as numeric, 4: And in the column formula we write: Count, and make a reference, between parentheses, to an attribute in the second level, such as FlightSeatLocation: Page29
13.03 The attribute FlightCapacity will not be physically created in the FLIGHT table because its global formula, and it will always count the number of seats on the flight. We then press F5. And we will see that the Flight physical table SEAT is created, Page30
associated with the second level of the Flight transaction, with the attributes and key we have defined and the structure of the FLIGHT table will not be modified because the attribute FlightCapacity will not be physically created as expected. We agree, so we proceed. We execute the Flight transaction... query flight #1 and record some seats in it: 1A - window 1B middle 1C aisle 1D window 1E middle 1F aisle Page31
2A window and we leave it here.. We should note that the seat count is updated everytime that we add a seat to the flight Page32
Let s go back to GeneXus. We also have other formulas of the Count style that do operations with several records, such as Sum Page33
Page34
Average and others. The attribute referenced between parentheses in the formula provides GeneXus with information on the table to be navigated for doing the calculation. If GeneXus detects a relation between the navigated table and the location where the formula attribute is defined, it will only take into account the related records for the calculation In this example, where FlightId is present both where the formula is defined, and where the navigation takes place, so only the seats of the flight we are in are counted and not all the seats recorded on the table. Page35
If the relation is not found, then GeneXus will do the operation considering all the records in the navigated table. Also, we can count or add or average certain records that fulfill an explicit condition we have indicated. By way of example, if we want to count the number of window seats on the flight, to the formula we add, inside the parentheses, a comma and the corresponding condition Because the FlightSeatLocation attribute is from the Location domain and it has these 3 enumerated values defined the syntax to require the value that the attribute has taken is: domain name, dot, and name associated with the value we want to filter Page36
Press F5, w ego to Flight transaction, query flight #1 and we see that the formula attribute was counted and now shows: 3, which corresponds to the number of window seats, which matches the entries we made on the seat grid. Page37
Lastly, we must keep in mind that, just like all Global formulas, these could also have a triggering condition. That means that we can add an if at the end of the definition, with a specific assessment, for the formula to be triggered only if that condition is true. Page38