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. Let’s focus on Global Formulas.
The Transaction attributes contain a property named Formula.
When a calculation is defined in this property 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. Suppose the pharmacy needs to know at all times how many registered products there are of each product type. So, let’s define a new attribute in the ProductType Transaction with the purpose of defining it as a global formula:
Now define the calculation associated with the ProductTypeProductQuantity Attribute.
GeneXus offers a formula called Count to calculate the pharmacy need (there are many others, like Sum, Average etc.).
So, with the focus positioned on the ProductTypeProductQuantity attribute, look at the Properties window and search for the Formula property. Complete the Formula property as shown:
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 Product table).
If there is no relation between the table that will be navigated (Product) and the context where the formula attribute is defined (ProductType), GeneXus will do the calculation considering all records in the navigated table. However, if GeneXus detects a relation (primary attributes with the same name) between the table that will be navigated (Product) and the context where the formula attribute is defined (ProductType), it will only consider the related records for the calculation. So, in this case, for each product type only products of that product type are counted.
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:
Execute the ProductType Transaction in order to see for each product type how the product quantity is always calculated at the time:
You can add more products in order to verify for each product type how the product quantity is always calculated at the time.