FormulaEval Namespace
The FormulaEval namespace provides classes and methods to evaluate dynamic formulas for
SObjects and Apex objects. Use the methods to avoid unnecessary DML statements to
recalculate formula field values or evaluate dynamic formula expressions.
When using a formula against an SObject or Apex object as the context object, the class methods or properties referenced by the formula must be global.
1Account myAcc = new Account(Name='123');
2 FormulaEval.FormulaInstance ff = Formula.builder()
3 .withType(Schema.Account.class)
4 .withReturnType(FormulaEval.FormulaReturnType.STRING)
5 .withFormula('name & " (" & website & ")"')
6 .build();
7
8//Use the list of field names returned by the getReferenced method to generate dynamic soql
9 String fieldNameList = String.join(ff.getReferencedFields(),',');
10 String queryStr = 'select ' + fieldNameList + ' from Account LIMIT 1'; //select name, website from Account
11 Account s = Database.query(queryStr);
12 system.debug(ff.evaluate(s));
13For usage notes, see Formula Evaluation in Apex.
The following are the classes and enums in the FormulaEval namespace.