Evaluate Dynamic Formulas in Apex GA
Salesforce's Spring '25 release introduces GA support to evaluate dynamic formulas directly within Apex, empowering Admins to define and test rule criteria as text without needing complex code or Setup. This capability solves longstanding challenges around configurable rule engines by allowing formula evaluation against specific records at runtime. Developers can leverage new FormulaEval classes to dynamically gather referenced fields and evaluate boolean or other formula expressions, making discount, eligibility, or conditional logic easier to implement and maintain. The article includes sample Apex code demonstrating a simple, flexible formula evaluator that streamlines admin-friendly criteria validation.
- Use FormulaEval to dynamically evaluate formulas against specific records in Apex.
- Admins can define rule criteria as plain text formulas without requiring Apex or Flow expertise.
- Leverage getReferencedFields to dynamically query only necessary fields for formula evaluation.
- Validate formulas on-the-fly to check syntax and value compatibility before deployment.
- This pattern simplifies configurable rule engines using standard Salesforce formula syntax.
Image created by ChatGPT4o based on a prompt by Bob Buzzard Introduction Like zip handling in Apex , the ability to evaluate dynamic formulas in Apex is Generally Available in the Spring '25 release of Salesforce. Unlike zip handling, which we've all been wanting/battling with for years, this might not be such an obvious win. It's definitely something I could have used a few times in my Salesforce career, mostly around rule engines. I've written several of these in my time, to do things like apply a discount to a price based on a number of attributes of a customer, their spend, and whether they have a contract with us. In order to avoid everyone having to become an Apex or Flow expert, rules are configured through custom settings or custom metadata types, but defining the criteria to execute rules is a bit more tricky to surface for Administrator configuration, especially if you need to look at the values contained by a specific record.