• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

How to Dynamically Evaluate Formulas in Salesforce Apex

How to Dynamically Evaluate Formulas in Salesforce Apex
  • June 19, 2025
  • Tech Blogger
  • Salesforce, Salesforce Admin Tutorial, Salesforce Developments Tutorial
  • 0

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?

  1. Real-Time Calculations

Formulas are evaluated instantly, reflecting up-to-date data without requiring database writes or additional fields.

  1. Dynamic Automation Logic

Add more dynamic logic in Flows, Validation Rules, or Apex classes without hardcoding specific expressions.

  1. Performance Optimization

Avoid the need to create multiple formula fields for one-off or transient calculations. This improves performance and minimizes storage use.

  1. Enhanced UI Experience

Use real-time formula results in Lightning components, Visualforce pages, or screen flows to present context-aware data to users.

  1. Better Decision-Making

Great for complex, rules-based decisions like lead scoring, credit approvals, or discount eligibility without adding new fields.

  1. Reduced Metadata Bloat

Instead of cluttering your org with formula fields that serve limited use cases, DFE allows on-demand evaluation for cleaner architecture.

  1. Simplified Maintenance

Business logic stored in formulas (e.g., in Custom Metadata) can be updated without deploying changes across multiple automations or Apex classes.

  1. 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.

Author Bio

Tech Blogger
+ Recent Posts
  • Comprehensive Overview of Salesforce Agent-Force Platform
    June 12, 2025
    Comprehensive Overview of Salesforce Agent-Force Platform
  • Salesforce GraphQL Revolutionizing Data Queries in CRM
    June 6, 2025
    Salesforce GraphQL: Revolutionizing Data Queries in CRM
  • Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
    May 29, 2025
    Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
  • Einstein Activity Capture Boosting Productivity & Relationship Intelligence
    May 22, 2025
    Einstein Activity Capture: Boosting Productivity & Relationship Intelligence
Tags: Salesforce
  • Next Comprehensive Overview of Salesforce Agent-Force Platform
Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

How to Dynamically Evaluate Formulas in Salesforce Apex
How to Dynamically Evaluate Formulas in Salesforce Apex June 19, 2025
Comprehensive Overview of Salesforce Agent-Force Platform
Comprehensive Overview of Salesforce Agent-Force Platform June 12, 2025
Salesforce GraphQL Revolutionizing Data Queries in CRM
Salesforce GraphQL: Revolutionizing Data Queries in CRM June 6, 2025

Copyright @2023 Merfantz Technologies, All rights reserved