Salesforce Spring ’25 makes it generally available to evaluate formula expressions in Apex using the FormulaEval namespace. This post shows how to build and evaluate dynamic formula instances, query the referenced fields, and practical use cases for admins and developers.
Overview
Spring ’25 introduces FormulaEval to Apex, allowing developers and administrators to evaluate formula expressions at runtime without creating formula fields. This is useful for rule engines, validation checks, and admin-configured expressions stored in metadata or custom settings.
Why this matters
Admins can now author familiar Salesforce formulas as plain text and have Apex evaluate them against a specific record. That removes the need to create temporary fields or force admins to become developers to test expressions. For developers, the API provides methods to discover referenced fields so you can build efficient dynamic queries.
Sample server-side method (Apex)
Below is the core Apex method used to evaluate a formula string against a given sObject record. The code shows building a Formula instance, retrieving referenced fields, querying the record, and evaluating the formula.
@AuraEnabled
public static String CheckFormula(String formulaStr, String sobjectType,
String returnTypeStr, Id recordId)
{
FormulaEval.FormulaReturnType returnType=
FormulaEval.FormulaReturnType.valueof(returnTypeStr);
FormulaEval.FormulaInstance formulaInstance = Formula.builder()
.withType(Type.forName(sobjectType))
.withReturnType(returnType)
.withFormula(formulaStr)
.build();
String fieldNameList = String.join(formulaInstance.getReferencedFields(),',');
String queryStr = 'select ' + fieldNameList + ' from ' + sobjectType +
' where id=:recordId LIMIT 1';
SObject s = Database.query(queryStr);
Object formulaResult=formulaInstance.evaluate(s);
return formulaResult.toString();
}
Key points and best practices
- Use Formula.builder() to construct the formula instance and specify the sObject type and return type.
- Call getReferencedFields() to get only the fields you need to query — avoids selecting unnecessary columns and keeps queries efficient.
- Validate formulas on a sample record (via admin UI) before deploying them into rule engines or metadata-driven logic.
- Handle evaluation errors: a successful evaluation may return true/false or a computed value; invalid formulas should be surfaced back to the admin for correction.
Use cases
Common scenarios where dynamic formula evaluation helps:
- Rule engines that allow admins to author criteria in familiar formula syntax.
- Configuration-driven discount or pricing logic stored in custom metadata.
- Admin-facing testing tools that let users validate formulas against live records without creating fields in Setup.
References
- Spring ’25 samples repository: https://github.com/keirbowden/Spring25
- Release notes: FormulaEval in Spring ’25 release notes
- API reference: FormulaEval Namespace (Apex Reference)
Conclusion
The FormulaEval API brings familiar formula syntax into Apex at runtime, making it easier to build configurable rule engines and admin-friendly validation tools. For Salesforce admins and developers, this reduces friction when testing and deploying logic that depends on record-specific criteria.
Why it matters for admins, developers, and business users: dynamic formula evaluation lets administrators express business rules in a familiar way, while developers can keep logic flexible and metadata-driven. Business users benefit from faster iteration and fewer deployments to test and adjust rules.








Leave a Reply