With the Spring ’25 release, Salesforce has introduced the ability to evaluate dynamic formulas directly in Apex using the new FormulaEval namespace. This enhancement provides developers with a flexible and efficient way to work with formulas programmatically—without having to rely on hardcoded formula fields or complex workarounds.
Previously, if developers needed to evaluate formulas dynamically, they often had to rely on custom logic, additional fields, or even external processing. Now, with this new feature, Salesforce allows us to create and execute formulas on the fly, significantly improving the efficiency and flexibility of Apex code.
New FormulaEval Methods
The FormulaEval class introduces several useful methods:
• FormulaEval.evaluate(formula, contextRecord); → Evaluates a formula string against a provided record.
• FormulaEval.validate(formula); → Validates whether a given formula is correctly structured.
These methods allow developers to dynamically define and evaluate formulas at runtime, rather than relying on static formula fields in Salesforce objects.
Practical Example: Custom Discount Calculations
Let’s consider a real-world use case where this feature can be incredibly useful.
Imagine an e-commerce company that offers custom discount structures to its customers. Instead of hardcoding discount logic, they want to allow business users to define discount formulas dynamically and store them in a custom object.
For instance, a manager might define a discount formula like:
IF(TotalAmount > 1000, TotalAmount * 0.1, TotalAmount * 0.05)
This formula applies a 10% discount if the order total exceeds $1000, otherwise, it applies 5%.
Implementation Example
Here’s how you can dynamically evaluate this formula in Apex:
Step 1: Retrieve the Formula from a Custom Object
Assume we have a Discount_Rules__c custom object where formulas are stored in a field called Formula__c.
Discount_Rules__c rule = [SELECT Formula__c FROM Discount_Rules__c WHERE Name = 'Standard Discount' LIMIT 1]; String formula = rule.Formula__c;
Step 2: Evaluate the Formula Against an Order Record
Order__c order = [SELECT Id, TotalAmount__c FROM Order__c WHERE Id = 'a1B6F000000XXXX' LIMIT 1]; Object result = FormulaEval.evaluate(formula, order); System.debug('Calculated Discount: ' + result); Step 3: Validate a Formula Before Using It Boolean isValid = FormulaEval.validate(formula); if (!isValid) { System.debug('Invalid formula detected!'); }
Why This Matters
This feature unlocks new possibilities for Salesforce developers:
• Dynamic business rules – Allow admins or business users to define formulas without modifying code.
• Enhanced automation – Use formulas in Apex for custom calculations, validation rules, or decision-making.
• Greater flexibility – Reduce the need for multiple custom fields by dynamically evaluating formulas only when needed.
Conclusion
With FormulaEval, Salesforce is making Apex more powerful and adaptable. Whether you’re working with pricing rules, commission structures, or risk calculations, this new feature can help you create smarter, more dynamic applications.
How do you plan to use dynamic formula evaluation in Apex? Let us know your thoughts!