Dynamic Formulas in Apex Salesforce
3/10/2025
Salesforce Summer '24: Dynamic Apex Formulas
Salesforce has introduced Dynamic Formulas in Apex as part of the Summer '24 Release, reducing the need for unnecessary DML statements when recalculating formula field values or evaluating dynamic formula expressions. This enhancement optimizes performance and brings more flexibility to Apex development.
Using Dynamic Apex Formula Fields in SOQL Queries
One of the major advantages of dynamic formulas is the ability to use them in SOQL queries. Below is an example demonstrating how to dynamically construct and evaluate a formula field in a query:
Account myAcc = new Account(Name='123');
FormulaEval.FormulaInstance ff = Formula.builder()
.withType(Schema.Account.class)
.withReturnType(FormulaEval.FormulaReturnType.STRING)
.withFormula('name & " (" & website & ")"')
.build();
// Retrieve referenced fields dynamically for SOQL
String fieldNameList = String.join(ff.getReferencedFields(), ',');
String queryStr = 'SELECT ' + fieldNameList + ' FROM Account LIMIT 1'; // Generates 'SELECT Name, Website FROM Account'
Account s = Database.query(queryStr);
System.debug(ff.evaluate(s));Key Takeaways:
Dynamic SOQL Generation: getReferencedFields() fetches the required fields dynamically.
Flexible Formula Evaluation: The formula dynamically evaluates without persisting changes to the database.
Recalculating Formula Field Values Dynamically
Dynamic Apex formulas allow developers to evaluate and recalculate formula field values without requiring database updates. Consider the following example using a custom class:
global class MotorYacht {
global Integer lengthInYards;
global Integer numOfGuestCabins;
global String name;
global Account owner;
}
MotorYacht aBoat = new MotorYacht();
aBoat.lengthInYards = 52;
aBoat.numOfGuestCabins = 4;
aBoat.name = 'RV Foo';
FormulaEval.FormulaInstance isItSuper = FormulaEval.FormulaBuilder.builder()
.withReturnType(FormulaEval.FormulaReturnType.STRING)
.withType(MotorYacht.class)
.withFormula('IF(lengthInYards < 100, "Not Super", "Super")')
.build();
System.debug(isItSuper.evaluate(aBoat)); // Outputs: "Not Super"
// Evaluating owner details dynamically
aBoat.owner = new Account(Name='Acme Watercraft', Site='New York');
FormulaEval.FormulaInstance ownerDetails = FormulaEval.FormulaBuilder.builder()
.withReturnType(FormulaEval.FormulaReturnType.STRING)
.withType(MotorYacht.class)
.withFormula('owner.Name & " (" & owner.Site & ")"')
.build();
System.debug(ownerDetails.evaluate(aBoat)); // Outputs: "Acme Watercraft (New York)"Key Takeaways:
No Need for DML Operations: Changes are dynamically evaluated without persisting them.
Custom Object Support: This feature extends beyond standard Salesforce objects.
Conclusion
Salesforce's Dynamic Apex Formulas make Apex code more efficient and flexible by allowing: ✅ Formula-based SOQL queries that dynamically adjust fields.
✅ Recalculation of formula values on Apex objects without database updates.
✅ Improved performance by reducing unnecessary DML statements.
With these new capabilities, developers can build more optimized and dynamic Salesforce solutions, enhancing both performance and maintainability. ????