Home SalesforceApex How to Dynamically Evaluate Formulas in Salesforce Apex?

How to Dynamically Evaluate Formulas in Salesforce Apex?

by Dhanik Lal Sahni
Dynamic Formula Evaluation in Salesforce

Salesforce’s Dynamic Formula Evaluation (DEF) is a powerful feature that evaluates formula expressions at runtime based on the context in which they are executed. This capability increases formulas’ flexibility and applicability across various Salesforce scenarios, such as Flows, Validation Rules, and Apex code. This feature is introduced in Salesforce Summer’24 as a Developer preview feature.

Dynamic Formula Evaluation enables developers and administrators to create and evaluate formula expressions in real-time, based on changing context or data within Salesforce. This differs from traditional static formulas, which are typically defined in fields or rules and evaluated only within the context for which they are configured.

Why We Need Dynamic Formula Evaluation in Salesforce?

1. Real-Time Calculation

DFE enables the calculation of formula fields in real-time, eliminating the need to store the results in the database. This is especially useful when formula calculations are complex or rely on frequently changing data, allowing users to see updated values right away.

2. Flexibility in Automation and Validation

The ability to dynamically evaluate formulas in processes, workflows, and validation rules makes it easier to create complex logic. Administrators, for example, can use dynamic formula evaluation to dynamically check conditions or compute values within Flow Builder, increasing the power and adaptability of their automation tools without hardcoding specific values.

3. Improved Application Performance

Because dynamically evaluated formulas do not require field updates or additional database storage, they can improve application performance, particularly when numerous calculations are required but do not need to be persistently stored.

4. Enhanced User Experience

Dynamic Formula Evaluation allows for more effective customization of user interfaces. For example, it can dynamically display calculated values on page layouts, Lightning components, and Visualforce pages, giving users more contextually relevant information.

5. Complex Decision-making

For applications that require complex decision-making capabilities (such as scoring systems, real-time analytics dashboards, or predictive insights), DFE can calculate values based on the most recent data inputs without batch processing or manual recalculation.

6. Reduced Field and Object Bloat

By calculating values on the fly, organizations can avoid creating multiple formula fields or custom fields that are only used for transient calculations, lowering the overall complexity and storage requirements of their Salesforce organization.

7. Easier Maintenance and Updates

When business logic changes, updating a centrally maintained formula is often easier and safer than revisiting multiple workflow rules, process builder processes, or Apex scripts that may contain the same logic.

8. Enhanced Debugging and Testing

In development and testing environments, the ability to dynamically evaluate formulas allows developers and QA engineers to quickly verify and debug formula logic without deploying changes or affecting live data.

Implementing Dynamic Formula Evaluation

Client XYZ has e-Commerce application that offers discounts on festivals or special days. Discounts can vary based on customer types, total sale values or any other custom logic.

We can use discount logic somewhere like custom settings or custom metadata types and calculate stored formulas using apex Dynamic Formula Evaluation.

Let us create a custom metadata type Discount Calculation Rule (DiscountCalculationRule__mdt) with the field Discount Formula (DiscountFormula__c) of text type. This field will store discount rules that can be added based on some category, department or customer type.

Dynamic Formula Evaluation - SalesforceCodex

Add some discount logic based on your requirements. You can also add the below logic for testing this feature.

IF(ISPICKVAL(Type,'New Customer'),5,10)

Create an apex class to evaluate the formula based on logic setup in the above custom metadata type.

Test Functionality

We can call above created discount calculator method with opportunity record id. If the opportunity type is new customer then it will return 5 otherwise it will return 10.


Based on your business requirement you can set up a formula and use this feature.

This feature is tested using version 61.


Dynamic Formula Evaluation thus provides a strong mechanism for Salesforce administrators and developers to create more dynamic, efficient, and user-friendly applications. It reduces the need for new custom fields, simplifies complex logic implementation, and improves the Salesforce platform’s overall ability to handle real-time data processing and user interaction scenarios.


Evaluate Dynamic Formulas in Apex (Developer Preview)

Formula Class

Related Posts

Implementing Apex Cursors for Optimal Resource Management in Salesforce

Efficient Ways to Debug Salesforce Platform Events

Appointment Assistance Real Time Location Url Generation

Setting Up Live Chat with Salesforce Messaging

Ultimate Guide to Monitoring Platform Events using Streaming Monitor

Need Help?

Need some kind of help in implementing this feature, connect on my LinkedIn profile Dhanik Lal Sahni.

You may also like

Leave a Comment