I have written a before update trigger on Opportunity to update some fields based on formula fields.

Due to some limitations, I could not create a formula field so created number fields and populate them using a trigger.

Since the calculation is based on some formula fields, I'm updating the values after every update.

The problem I'm facing is it's changing the value after every save even when I'm not changing any value. Ex: if my earlier value was 19.25 now it's changing it to 19.24.

What I'm guessing is it calculates the value after every save but because of many formulae, it adjusts the value by some fraction.

How can I keep my values as it is if there is not change? I tried comparing old value and new value but in that case it's not updating at all.

for(Opportunity opp : oppList){

    if(opp.Total_Cost_Formula__c != oldMap.get(opp.Id).Total_Cost_Formula__c){
       opp.unit_sales_price__c  = 1.60 * opp.unit_total_cost__c;
  • Do you mean the formula field value is changed upon every save action (even if the dependent fields were not edited)? – arut Aug 28 '20 at 5:00
  • the value in number field, which in my case unit_sales_price__c – sfdc Aug 28 '20 at 11:12
  • So, there is no change in Total_Cost_Formula__c value (meaning the fields that determine this formula field have also not changed), but any other save action on the record is causing the trigger to recalculate and update unit_sales_price__c with a new value every time. Is this a correct understanding? – arut Aug 28 '20 at 11:18
  • Yes, that's correct – sfdc Aug 29 '20 at 14:28
  • I'm assuming that the datatype of all the fields is NUMBER with 2 decimal places. Are you using any math functions like ROUND or CEILING anywhere (including the formula field)? Unless something else is impacting one of these fields, this problem would not happen. – arut Aug 29 '20 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.