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.

Account myAcc = new Account(Name='123');
       FormulaEval.FormulaInstance ff = Formula.builder()
           .withType(Schema.Account.class)
           .withReturnType(FormulaEval.FormulaReturnType.STRING)
           .withFormula('name & " (" & website & ")"')
           .build();
 
//Use the list of field names returned by the getReferenced method to generate dynamic soql
       String fieldNameList = String.join(ff.getReferencedFields(),',');
       String queryStr = 'select ' + fieldNameList + ' from Account LIMIT 1'; //select name, website from Account
       Account s = Database.query(queryStr);
       system.debug(ff.evaluate(s));

The following are the classes and enums in the FormulaEval namespace.