+ Start a Discussion
cmarz_1cmarz_1 

Update formula field without saving?

I'm building an edit page for Opportunity Line items.  If a users enters 2 in the QTY field and $10 in the Sell price field I'd like to total field (read only formula field) to update to $20.  Is there any method I could call to get the total to update without saving?  I want to allow the user to be able to cancel and not save their changes hence the reason for not wanting to save after the field values have been changed.
Best Answer chosen by Admin (Salesforce Developers) 
BrianWKBrianWK

There's a third option that you're not considering. And that's fool them!

 

Since you're using a custom controller there's nothing preventing you from using that formula field and replacing with a different field that "Acts" like the formula field.

 

I've done this myself. I have a Total amount on our custom "Contract Products" amount. So what I do is I create a new Opportunity in the controller, and I use the Amount of the opportunity as a replacement for the Contract Product's total field.

 

So I have a getFakeOpportunity and SetFakeOpportunity method in the custom controller. Then in the getFakeOpportunity I set FakeOpportunity.Amount to equal the Contract_product__c.Quantity__c * Contract_product__c.Annual_Price__c.

 

On the page I use an Outputfield component to FakeOpportunity.Amount. Then on the Inputfields for Quantity__c and Annual_Price__c I add an Action Support to re-render the outputpanel the WHOLE object is sitting in on change.

 

And now it looks like you have the Formula field (thanks to an outputlabel) but it really is a fake field you populate just for the sake of the user. But now they can preview the output - and when you save the formula field calculates so it appears "seamless".

All Answers

BrianWKBrianWK

Cmarz,

 

I'm not sure if you can do this with a standardcontroller only - all the examples I've done have been extensions or custom controllers.

 

But you can add an action support component to the relevant fields (QTY and sell Price) so that it rerenders your total field (or the whole section may be better) when the value changes.

 

I do this on some of my own pages, but like I said the field that's being calculated in a get method in the custom controller. I don't know if the Total field - if a formula field - would be updated until the value is written to the database.

cmarz_1cmarz_1
Thanks Brian, I am actually using a custom controller but I can't set the value of a formula field in the contoller.  I guess I'll either have to leave it (and confuse my sales people) or force a save (and anger my sales people when they want to use the cancel feature and it doesn't work).
BrianWKBrianWK

There's a third option that you're not considering. And that's fool them!

 

Since you're using a custom controller there's nothing preventing you from using that formula field and replacing with a different field that "Acts" like the formula field.

 

I've done this myself. I have a Total amount on our custom "Contract Products" amount. So what I do is I create a new Opportunity in the controller, and I use the Amount of the opportunity as a replacement for the Contract Product's total field.

 

So I have a getFakeOpportunity and SetFakeOpportunity method in the custom controller. Then in the getFakeOpportunity I set FakeOpportunity.Amount to equal the Contract_product__c.Quantity__c * Contract_product__c.Annual_Price__c.

 

On the page I use an Outputfield component to FakeOpportunity.Amount. Then on the Inputfields for Quantity__c and Annual_Price__c I add an Action Support to re-render the outputpanel the WHOLE object is sitting in on change.

 

And now it looks like you have the Formula field (thanks to an outputlabel) but it really is a fake field you populate just for the sake of the user. But now they can preview the output - and when you save the formula field calculates so it appears "seamless".

This was selected as the best answer
cmarz_1cmarz_1
Good idea thanks!