I have an “invoice” custom object which has a couple of summarising formula fields in. I also have a complicated and dynamic visualforce page for building the invoice objects.
As the user is completing this form, different areas are dynamically updated to show “latest” values. This is all fine where I am copying data about or performing bespoke lookups and maths, but – in what is really the simplest case – a formula field, I can find no way to get the value to update, without performing some weird cyclic save + reload of the whole object.
This is inappropriate for a couple of reasons (the object should not be saved until the invoice is complete + accepted) and the other option I have found online is to spoof the value by duplicating my formula field maths into the custom controller and re-executing them there – also unacceptable from a code/logic location/duplication perspective.
Is there anyway to “force” an object/page to recalculate it’s formula fields, either through standard or extended controllers?
Good news everybody!
This has just been added to the Winter ’16 release. It can be used like this:
From the Documentation:
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.
Note that this method doesn’t recalculate cross-object formulas, only ‘local’ formula’s referring to the record itself!