
Introduction
Dynamic Formula Evaluation (DFE) is a powerful enhancement introduced in Salesforce Summer ’24 as a Developer Preview feature. It empowers developers and admins to evaluate formula expressions at runtime based on the current context—whether it’s in Apex, Flows, Validation Rules, or custom automations.
Unlike traditional formula fields that are statically tied to object definitions and persist in the database, DFE evaluates formulas on the fly without requiring permanent field creation or data storage. This opens up tremendous flexibility in handling real-time logic across the platform.
Why Use Dynamic Formula Evaluation?
- Real-Time Calculations
Formulas are evaluated instantly, reflecting up-to-date data without requiring database writes or additional fields.
- Dynamic Automation Logic
Add more dynamic logic in Flows, Validation Rules, or Apex classes without hardcoding specific expressions.
- Performance Optimization
Avoid the need to create multiple formula fields for one-off or transient calculations. This improves performance and minimizes storage use.
- Enhanced UI Experience
Use real-time formula results in Lightning components, Visualforce pages, or screen flows to present context-aware data to users.
- Better Decision-Making
Great for complex, rules-based decisions like lead scoring, credit approvals, or discount eligibility without adding new fields.
- Reduced Metadata Bloat
Instead of cluttering your org with formula fields that serve limited use cases, DFE allows on-demand evaluation for cleaner architecture.
- Simplified Maintenance
Business logic stored in formulas (e.g., in Custom Metadata) can be updated without deploying changes across multiple automations or Apex classes.
- Faster Debugging
Quickly test or simulate formula logic in Apex or unit tests without touching actual data or deployment pipelines.
Implementation: Use Case 1 – Dynamic Discount Calculation
Scenario:
Client XYZ runs an e-commerce business and wants to offer conditional discounts based on criteria such as customer type or special promotions. These rules change frequently, and storing them in formula fields isn’t ideal.
Solution:
Store formula logic in Custom Metadata and evaluate it dynamically using Apex and the FormulaEval class.
Step 1: Create Custom Metadata Type
- Name: DiscountCalculationRule__mdt
- Field: DiscountFormula__c (Text)
- Example Value:
IF(ISPICKVAL(Type,’New Customer’), 5, 10)
Step 2: Apex Class to Evaluate Formula
public class DiscountCalculator {
public static Decimal getOpportunityDiscount(String recordId) {
Opportunity opp = [
SELECT Id, Type FROM Opportunity WHERE Id = :recordId
];
return DiscountCalculatorService.calculateDiscount(opp);
}
}
global class DiscountCalculatorService {
public static Decimal calculateDiscount(Opportunity opt) {
DiscountCalculationRule__mdt discountRule = [
SELECT DiscountFormula__c
FROM DiscountCalculationRule__mdt
WHERE DeveloperName = ‘StandardDiscount’
LIMIT 1
];
FormulaEval.FormulaBuilder builder = Formula.builder()
.withType(Opportunity.SObjectType)
.withReturnType(FormulaEval.FormulaReturnType.INTEGER)
.withFormula(discountRule.DiscountFormula__c);
FormulaEval.FormulaInstance instance = builder.build();
return (Decimal)instance.evaluate(opt);
}
}
Example Output
Decimal discount = DiscountCalculator.getOpportunityDiscount(‘006Ws000001O5z8IAC’);
System.debug(‘Discount: ‘ + discount);
If the Opportunity’s Type is ‘New Customer’, it returns 5; otherwise, it returns 10.
Additional Example: Dynamic Lead Scoring
Scenario:
Your organization uses a custom lead scoring model that frequently evolves based on marketing strategy. Rather than updating Apex logic repeatedly, store scoring rules as formulas.
Example Formula Stored in Metadata:
IF(AND(ISPICKVAL(Industry, ‘Technology’), AnnualRevenue > 1000000), 90, 50)
Evaluate in Apex:
Lead lead = [SELECT Id, Industry, AnnualRevenue FROM Lead WHERE Id = :leadId];
FormulaEval.FormulaBuilder builder = Formula.builder()
.withType(Lead.SObjectType)
.withReturnType(FormulaEval.FormulaReturnType.INTEGER)
.withFormula(metadataFormula);
FormulaEval.FormulaInstance instance = builder.build();
Integer score = (Integer) instance.evaluate(lead);
Version Support
Dynamic Formula Evaluation is available starting in API Version 61.0. Make sure your Apex classes or managed packages are using this or a higher version to leverage the feature.
See how FieldAx can transform your Field Operations.
Try it today! Book Demo
You are one click away from your customized FieldAx Demo!
Final Thoughts
Dynamic Formula Evaluation is a game-changer for Salesforce developers and architects. It offers unmatched flexibility, reduces the need for redundant metadata, and makes it easier to adapt business logic to real-world changes.
Use it to:
- Simplify automation logic
- Reduce field bloat
- Customize UI experiences
- Improve performance
Whether you’re building lead scoring models, applying discounts, validating inputs, or customizing dynamic flows—DFE empowers you to do more with less.