2

Is there anyway to force an object to recalculate it's formula fields, either through standard or extended controllers in visualforce page(Mostly using action:suppot). While googling i found in Winter 16 release recalculateFormulas() method is introduced.. But i dont know how to use it. Has anybody used this method.

1 Answer 1

4

To get the updated values for a formula you have two choices:

  1. Do a DML and update the record, then require for the values
  2. In winter 16 you can use recalculateFormulas()...

i.e Suppose the formula field fField__c on Account is the first three characters of the name.

Account a = [Select ID, Name, fField__c From Account where.....];
a.Name = '123AAAAAAAA';
a.recalculateFormulas();
system.assertEquals('123',a.fField__c);

The recalculateFormulas() method acts on an sObject to recalculate the formulas for the specific sObject without having to do a DML. The change is not persisted to the database though unless you do a dml.

Recalculates all formula fields on an sObject, and sets updated field values. Rather than inserting or updating objects each time you want to test changes to your formula logic, call this method and inspect your new field values. Then make further logic changes as needed

4
  • Additional note: This method doesn’t recalculate cross-object formulas. If you call this method on objects that have both cross-object and non-cross-object formula fields, only the non-cross-object formula fields are recalculated.
    – Himanshu
    Nov 12, 2015 at 13:53
  • @Himanshu - While you may be correct, the documentation does not state that. is says all formula fields. Although there are lots of undocumented exceptions as we know. I have not tested it so YMMV
    – Eric
    Nov 12, 2015 at 13:54
  • 1
    You may be correct too. It is mentioned in usages section developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – Himanshu
    Nov 12, 2015 at 13:57
  • @Himanshu - Great point and you are correct. Thanks for the info on the clickthrough....
    – Eric
    Nov 12, 2015 at 13:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .