Formula Evaluation in Apex
Formula evaluation in Apex helps avoid unnecessary DML statements to recalculate
formula field values and evaluate dynamic formula expressions. Dynamic formulas in Apex
support SObjects and Apex objects as context objects. The context type that corresponds to
the Apex class used in the FormulaBuilder.withType()
method must be a global, user-defined Apex class. Any fields, properties, or methods that
the formula references must also be global.
Formulas in Apex support these features.
- Reference Apex types in formula fields. The values contained in individual components of such Apex types are accessed and evaluated by the formula. Address, Location, URL, and UUID System types are supported.
- Reference standard lookups and custom lookups in formula fields.
- Access polymorphic relationship fields.
- Access the return value from the toString() method in formula fields.
Formula evaluation in Apex is bound by the formula field character limit, but not the compile size limit. A formula can contain up to 3,900 characters including spaces, return characters, and comments.
Formula functions that are available to use in Apex are ones that can be used in validation rules. For details, see Formula Operators and Functions by Context.