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;
}
}
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 updateunit_sales_price__c
with a new value every time. Is this a correct understanding? – arut Aug 28 '20 at 11:18NUMBER
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