Skip to main content
New tool CRON Expression Builder — preview next run times before you schedule Apex. Open the builder →
Recalculate Formula Fields without DML Statement - Salesforce developer tutorial
Apex

Recalculate Formula Fields without DML Statement

The short answer

Formula.recalculateFormulas(list<sObject>) recalculates formula fields on in-memory records, so no insert or update DML is needed. The calculated values are then readable in Apex, which lets a Visualforce page, Lightning Component, or Lightning Web Component show them before the record is saved.

Key takeaways Call Formula.recalculateFormulas(list) to recalculate formula fields on in-memory records without an insert or an update. Pass in a list of populated SObject instances and their formula fields are filled in straight away. Read the results in Apex to show calculated values in Visualforce, Lightning Components, or Lightning Web Components before the record is saved.

Sometimes we need to show output of formula in UI. when I say UI, it can be either via Visualforce Pages, Lightning Components or Lightning Web Components. Traditionally to get the formulas calculated, DML needs to be performed for the objects. Since winter 20 release, Salesforce has provided callability to auto calculate formulas without performing DML operation.

Scenario

We have a custom formula field "Opportunity_Serial_Number__c" on opportunity line item to show opportunity Serial Number, We have following apex code:

private void showTotalAmountOnOLI(id OppId, id PBEId) {
	List<OpportunityLineItem> OLIs = new List<OpportunityLineItem>();
        OLIs.add(new OpportunityLineItem(OpportunityId = OppId, PricebookEntryId = PBEId, UnitPrice = 100, Quantity = 10));
        Formula.recalculateFormulas(OLIs);
        system.debug(OLIs[0].Opportunity_Serial_Number__c);
}

Explanation

  • We have showTotalAmountOnOLI method accepting OppId (Opportunity Id) and PBEId (Pricebook Entry Id).
  • Initialized list of opportunity with one line item providing opportunity id and pricebook entry id.
  • Without writing an insert DML statement, we forced recalculation of all formula fields for the records passed in function recalculateFormulas();
  • Syntax of function is Formula.recalculateFormulas(list). More information is in the System.Formula class documentation.
  • After recalculating the formulas, Opportunity Serial Number printed with debug statement.

This is just an explaination how recalculating works, however this is very much needed when showing output of formula field on visualforce page without saving a record.

Frequently asked questions

How do you calculate formula fields in Apex without DML?

Pass a list of SObjects to Formula.recalculateFormulas(list<sObject>). It evaluates the formula fields on those in-memory records, so you never write an insert or update DML statement.

What is the syntax for Formula.recalculateFormulas in Salesforce Apex?

The syntax is Formula.recalculateFormulas(list<sObject>). It takes a list of SObject records and evaluates the formula fields on them.

Why recalculate formula fields without saving a record in Salesforce?

It lets you show the formula value in a Visualforce page, Lightning Component, or Lightning Web Component before the record is committed to the database.

Newsletter

One email every Tuesday

New guides, tool updates, and the release-note changes that break things.

No spam. Unsubscribe in one click.

Comments

Loading comments...

Leave a Comment