Salesforce Cookies #17

      No Comments on Salesforce Cookies #17

Recalculating Formula on a record without even querying the fields

When you’re working in Triggers or in apex code, you may want to get a recalculated formula field values on a particular object. This can be done with the help of a method in the Formula class. Let’s have a look on syntax and an example to understand it.

Custom_Object__c oC = new Custom_Object__c();
oc.First_Name__c = 'Naval';
oc.Last_Name__c = 'Sharma';

// We will need to get the value of Full_Name__c field which is a
// formula field in actual, so we will use Formula.recalculateFormulas 
// method
List<Custom_Object__c> lstOCustom = new List<Custom_Object__c>{ oC };
List<FormulaRecalcResult> lstFRResult = Formula.recalculateFormulas(lstOCustom);

if(lstFRResult[0].isSuccess()) {
   System.debug('Full Name: ' + lstOCustom[0].get('Full_Name__c'));

Things to consider while using this method

  • While calculating the formula it will be using other field values and if all the data is not present on the SObjects then it will be going to load that data and which may result in the change in your transactional limits.
  • If you have N number of records in the list passed to the method then resulted list will have the same N number records in the same order in which they were passed.
  • You can either use the getSObject() or same SObject record which were passed to get the recalculated formula values.


You can have a look at the following link for more details

About Naval Sharma

I am certified Salesforce Developer with Sales and Service cloud certifications. I am a true believer in #GivingBack principle and a Salesforce enthusiast. I am working as a member of our #SalesforceOhana to learn and help other fellow Trailblazers of the community to grow and learn. "I'm a true believer in karma. You get what you give, whether it's bad or good."

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.