What are formulas?
Applications are often required to make calculations that involve the values of specific attributes, constants and/or functions. For all these cases, GeneXus offers Formulas.
There are different possible ways to define formulas.
GLOBALLY: the calculation will be available throughout the Knowledge Base;
LOCALLY or INLINE: in this case, the calculation will be available only in the object in which it has been defined.
Let’s start by learning what a Global Formula is. A global formula is a calculation the developer defines in association with an attribute. Note that the transaction structures contain a column labeled Formula:
When a calculation is defined in this column for an attribute, this means that the attribute is virtual. In other words, it will not be created physically as a field in a table because the value of the attribute will be obtained every time it is needed by doing the calculation.
Let's see this with an example.
Let’s suppose the Travel Agency needs to know at all times how many registered attractions there are of each category. So, let’s define a new attribute in the Category transaction with the purpose of defining it as a global formula:
Let’s now define the calculation associated to the CategoryAttractions attribute.
GeneXus offers a formula called Count to calculate the Travel Agency need (there are many others, like Sum, Average, etc.).
The attribute referenced inside the parenthesis of the formula provides GeneXus with the information of the table to be navigated to do the calculation (in the definition above, GeneXus knows that it has to count in the Attraction table). Then, if GeneXus detects a relation between the table it will navigate (Attraction) and the context where the formula attribute is defined (Category), it will only consider the related records for the calculation. In this example, CateogryId is present in both contexts: where the formula is defined and in the table to be navigated for doing the calculation of the formula. So, only attractions of each category are counted and not all attractions recorded in the navigated table will be considered. If no relation is found, then GeneXus will do the calculation considering all records in the navigated table.
Let’s press F5. You can see that no physical changes will be made to the database. GeneXus will only generate some programs and you will get the Developer Menu running again:
Let’s execute the Category transaction in order to see for each category how the attractions quantity is always calculated at the time:
Every attribute defined as a global formula will be read-only, and it will not be possible to enter a value for it. This happens because the attribute obtains its value from the associated calculation, which is run every time the attribute is used.
For this reason, there isn't a field in the physical table to store this attribute value. For this reason, there's no need for it to be editable.
You can add more attractions in order to verify for each category how the attractions quantity is always calculated at the time.