Dynamic Formulas in Apex is reshaping how Salesforce professionals work — and this article breaks down everything you need to know.
Have you ever had to create a “technical” formula field just so you could pull a value into a trigger or a flow? It is a common workaround, but it clutters your schema and makes maintenance a headache. With the Spring ’25 release, we finally have Dynamic Formulas in Apex as a generally available feature, and it is going to change how you handle business logic.
Look, we have all been there. You have a complex rule engine or a pricing tool, and you want admins to be able to tweak the logic without asking a developer for a deployment. Before now, that usually meant building a messy parser or relying on those hidden fields. Now, you can use the FormulaEval namespace to run formula strings directly in your code.
Why You Should Care About Dynamic Formulas in Apex
The real power here is flexibility. In my experience, the best Salesforce setups are the ones where developers build the engine and admins provide the fuel. By using Dynamic Formulas in Apex, you can store formula logic in Custom Metadata or Custom Settings. When the business changes a discount rule, an admin just updates a text field, and your Apex picks it up instantly.
But it is not just about making things easier for admins. This feature is a huge win for performance too. When you use the formula builder, you can actually see which fields the formula needs before you even run it. This means you aren’t stuck querying every single field on an object just in case the formula needs it. When deciding between Apex vs Flow for complex logic, this new capability might push you toward code if you need highly configurable rules that stay fast.

How to Implement Dynamic Formulas in Apex
So how do you actually get this working? The process is pretty straightforward. You use a builder pattern to define your formula, tell it what object type you are working with, and specify what kind of result you expect back – like a String, Boolean, or Number.
Here is a simple example of how I’ve been setting this up in my projects. This method takes a formula string and a record ID, then does the heavy lifting of figuring out which fields to query and then evaluating the result.
@AuraEnabled
public static String evaluateRule(String formulaStr, String sobjectType, String returnTypeStr, Id recordId) {
// Set up the return type
FormulaEval.FormulaReturnType returnType = FormulaEval.FormulaReturnType.valueOf(returnTypeStr);
// Build the formula instance
FormulaEval.FormulaInstance instance = Formula.builder()
.withType(Type.forName(sobjectType))
.withReturnType(returnType)
.withFormula(formulaStr)
.build();
// Get the fields we actually need to query
List<String> fields = instance.getReferencedFields();
String query = 'SELECT ' + String.join(fields, ',') + ' FROM ' + sobjectType + ' WHERE Id = :recordId LIMIT 1';
SObject record = Database.query(query);
// Run the formula against the record
Object result = instance.evaluate(record);
return String.valueOf(result);
}One thing that trips people up is the return type. Make sure your Apex code and your formula string are in sync. If your formula returns a Boolean but your code expects a Decimal, you’re going to have a bad time.
Practical Tips for Success
I have seen teams try to over-engineer this, but the trick is to keep it simple. Start by using getReferencedFields(). It is probably the most overlooked part of this feature. If you hard-code your queries, you lose half the benefit of using Dynamic Formulas in Apex. Let the formula tell you what it needs.
Another thing to keep in mind is error handling. Formulas can fail if the syntax is wrong or if a field is missing. Always wrap your evaluation in a try-catch block. You don’t want a typo in a Custom Metadata record to crash your entire Apex Trigger or user interface.
Key Takeaways
- Dynamic Formulas in Apex allow you to evaluate logic at runtime without extra fields.
- Use the
Formula.builder()method to define your object type and return type. - Always call
getReferencedFields()to keep your SOQL queries efficient. - This is perfect for rule engines, dynamic pricing, and custom validation tools.
- Handle errors gracefully so admin mistakes don’t break the system.
Wrapping Up
This feature has been a long time coming. Honestly, most teams get bogged down by “formula bloat,” and this is the perfect way to clean that up. It lets you keep your logic where it belongs – in a configuration layer – while keeping your Apex code clean and reusable.
If you are looking to build tools that are easy for admins to manage but still have the power of code, you need to start playing with Dynamic Formulas in Apex today. It is one of those rare updates that actually makes everyone’s life easier, from the dev writing the code to the business user waiting for a new feature.








Leave a Reply