Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

Unfortunately not. Formula fields aren't stored in a Salesforce record, rather the formula definition is executed at the time that the record is queried from the database. So its the act of retrieval that populates the field based on the formula.

Another option is to save the record, query it from the database including the formula fields, capture the formula fields into controller properties or similar and then delete the record. Its clunky but it will allow you to get at the formula fields without leaving a half-populated record in the database.

share|improve this answer
1  
Another option is to set a savepoint, update/insert the record, then do a rollback. –  Ralph Oct 30 '12 at 6:13
    
I think this is as close as I'm going to get. I didn't want to have to update the record every time a value was tweeked, because it makes the concept of "cancelling" out of the step redundant, because you arn't cancelling any changes as the user would expect. I have added an upsert to the onchange function and then re-query the formula fields in the component controller. I will have to think of a new way to then allow the user to revert out to the state the object was in before they entered the page. shouldn't be too hard! :) –  Simon Lawrence Oct 30 '12 at 10:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.