Formulas are expressions that contain a calculation which, once evaluated, returns certain value. They are like functions.
In addition, they are a key way to share knowledge, when used to define the way everytime an attribute is going to be calculated. In such a case, the attribute is not stored in database (unless otherwise stated). We know that special kind of formulas as "global formulas" or "formula attributes".
What is a Formula#
An expression that once evaluated, returns certain value. For example:
&today.Year()  PersonBirthDate.Year()
where the formula calculates the person age, from: &today system variable, PersonBirthDate attribute, and the Year method. Or:
Sum( InvoiceTotal, InvoiceDate >= &fromDate )
where a sum of the values of the InvoiceTotal attribute is going to be triggered, for all records of the Invoice table whose InvoiceDate is bigger or equal than &fromDate variable value.
Thus, a formula is a calculation based on other attributes, constants and/or functions or methods.
When an attribute or variable value can be calculated in this way, it can be associated to a formula. In addition, when this assignment is done at attribute definition level (that is, for the attribute, inside the transaction structure where it is specified), the attribue itself is known as "formula attribute" or abbreviated "formula".
There are two ways to define formulas:
 Global Formulas (Global definition, at the Knowledge Base level)
 Local / Inline Formulas (Local definition, within objects code)
Classification of Formulas by navigation type#
Formulas can be classified in three groups, depending on the type of calculation needed. This classification is valid either for global or local formulas.
 Horizontal Formulas: Allow defining many kind of expressions (such as arithmetic, among others) involving a single record and its associated in an extended table.
 Aggregate Formulas: Allow defining some calculations or searches involving many records of a table (and its associated by means of the extended table).
 Compound Formulas: Several horizontal and/or aggregate expressions combined.
All these kind of formulas may have trigger conditions.
Note: You don't need to indicate when defining a formula in GeneXus, if it belongs to one classification or another. This is only an external classification.
Base table of a Formula#
The table that is navegated in order to evaluate the formula is known as its base table. Depending on the kind of formula, the evaluation will consider only one record (horizontal formula) or many (aggregate formula). In the former (horizontal), the context in what the formula is, will afect the base table determination. In the latter, the context will afect the filters that will be applied in order to retrieve the meaningful records.
Optimized generated code#
Defining formulas it's even better than writing procedures and invoking them.
When you define a formula, GeneXus has the knowledge of its definition and is able to generate optimized sentences by combining the formula query with the query in which the formula is present.
On the other hand, if you define a procedure and you invoke it, the procedure code is not "visible" from the invoker, and GeneXus can't combine the knowdlege and generate the most optimized code.
See Also#
Formulas/Generating SQL
Changes in Formulas terminology
