3

I have two fields on a vf page.

1) Bid Price - It is a input field of type currency. This field is used in the formula of the second field below.

2) Net Price - This is a formula field which calculated formula on the basis of bid price.

Requirement: I need to refresh the value of 'Net Price' whenever the 'Bid Price' value is input on a v.f page. As of now it remains constant to the value which it has fetched upon editing the page.

enter image description here

Below is the code snippet of the 'Bid Price' and 'Net Price' fields columns of the VF Page.

<apex:column >
    <apex:facet name="header">Bid Price</apex:facet>
    <apex:inputField value="{!item.itemRecord.Bid_Price__c}" style="width: 50px" rendered="{!NOT(blnMidCycle)}"/>
    <apex:outputField value="{!item.itemRecord.Bid_Price__c}" style="width: 50px" rendered="{!blnMidCycle}"/>                                    
</apex:column>

<apex:column headerValue="Net Price">
    <apex:outputField value="{!item.itemRecord.Net_Price__c}" style="width: 50px"/>                                   
</apex:column>

How can I modify the above code to refresh the 'Net Price' field whenever the 'Bid Price' field value is changed on the vf page ?

EDIT : Below is the formula of my field.

IF( Apttus_QPConfig__PriceListId__r.Resale_Flag__c = TRUE , Bid_Price__c -(Bid_Price__c * (Apttus_QPConfig__PriceListId__r.Resale_Maximum_Margin__c / 100)) , Bid_Price__c)
2
  • Did your problem solved. Commented Aug 12, 2016 at 6:40
  • No...I am not familiar with the java script...so I am trying to figure out the same. The formula field calculates the value on the basis of IF condition. Not sure if I can do this with java script. Adding the formula for the field in my question edit.
    – Ruchi
    Commented Aug 12, 2016 at 7:11

1 Answer 1

2

That is not possible untill you save the record.

So Now you have two options here.

  1. Save the record when user change the input field value.

  2. Instead of reffering that formula field on UI use the formula calculation on UI to display updated value.

Eg: so if your formula do Net price = Bid Price * 100 then on VF page just do Bid Price * 100 don't refer the field.

So your formula is

IF( Apttus_QPConfig__PriceListId__r.Resale_Flag__c = TRUE , Bid_Price__c -(Bid_Price__c * (Apttus_QPConfig__PriceListId__r.Resale_Maximum_Margin__c / 100)) , Bid_Price__c)

Update: 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);

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.

8
  • To do 2nd point , i'll have to calculate the value of the field in my controller and then reference it in v.f ? Or is there any way to directly calculate the value in the v.f page ?
    – Ruchi
    Commented Aug 11, 2016 at 9:59
  • @Ruchi if its a simple calculation you can do in javascript then go for VF otherwise use controller Commented Aug 11, 2016 at 11:37
  • Thanks @Tushar for your suggestion. I will give it a try.
    – Ruchi
    Commented Aug 11, 2016 at 12:25
  • Why not use the recalculateFormulas() method on the object, and just refresh the value of the field from there without performing dml? BTW, I've never tried it, but it looks like it could work
    – Nick C
    Commented Aug 12, 2016 at 7:26
  • @NickCook : Can you please provide me any reference how this is used ?
    – Ruchi
    Commented Aug 12, 2016 at 8:00

You must log in to answer this question.

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