Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
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, AccessLevel.USER_MODE);
12 System.debug(ff.evaluate(s));For usage notes, see Formula Evaluation in Apex.
The following are the classes and enums in the FormulaEval namespace.