Search

Sunday 25 October 2015

A Useful Winter 16 Function

Not many people will notice a small function in the winter 16 release which has potential to help the performance considerable of the entire platform, if we all use this function wisely.

System.SObject Class

recalculateFormulas()
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.

For example :

You want to insert an Account in a testmethod and you are wanting to test that your formulas will be calculated correctly. Previously you would have to perform a DML. And we all know how expensive DMLs are for the platform. This little formula bypasses the need to do the DML.

Say your Account is quite basic and has several formula fields.



Account acc = new Account(Name='Steves Test');

//Now test a formula field StevesFormula__c to have the "This is a test" as the value without doing a DML

acc. recalculateFormulas();

system.assert(acc.StevesFormula__c == ' This is a test');




No comments:

Post a Comment

Note: only a member of this blog may post a comment.