2

I can't figure out why this formula is not recalculating. I've even called sObject.RecalculateFormulas and set up a debug log to see what's happening.

The field in particular is Annual_Charge_Amount__c

enter image description here

Debug log of database values:

Execute Anonymous: List<OpportunityLineItem> oli = [Select Id, TotalPrice, no_of_Months__c, Annual_Charge_Amount__c from OpportunityLineItem where Id = '00kj0000002xci8AAA'];
Execute Anonymous: oli[0].recalculateFormulas();
Execute Anonymous: System.debug('no_of_Months__c ' + oli[0].no_of_Months__c);
Execute Anonymous: System.debug('TotalPrice ' + oli[0].TotalPrice);
Execute Anonymous: System.debug('calc Annual_Charge_Amount__c ' + oli[0].TotalPrice / oli[0].no_of_months__c * 12);
Execute Anonymous: System.debug('actual Annual_Charge_Amount__c ' + oli[0].Annual_Charge_Amount__c);

12:20:04.2 (84302908)|USER_DEBUG|[5]|DEBUG|no_of_Months__c 46
12:20:04.2 (84402674)|USER_DEBUG|[6]|DEBUG|TotalPrice 348128.00
12:20:04.2 (84634146)|USER_DEBUG|[7]|DEBUG|calc Annual_Charge_Amount__c 90816.00
12:20:04.2 (84757614)|USER_DEBUG|[8]|DEBUG|actual Annual_Charge_Amount__c 91746.76

What exactly is going on? Could something be interrupting the update of this formula field?

Edit - added formula for no_of_Months__c no_of_months__c formula

1 Answer 1

2

I suspect that this has to do with rounding. For example, if No_of_Months has a scale of 0 (whole numbers only), but the months was actually calculated to be about 45.533335455115799184625157335256, perhaps because it was set by some other formula or workflow, you'd end up with a display value of 46, but this would cause rounding errors. If No_of_months is 45.533335455115799184625157335256, then your formula evaluates to 91746.760000000000000000000000001. Make sure that TotalPrice is indeed exactly 348128.00, and that no_of_months is also exactly 46. You might need to adjust whatever code is setting No_Of_months and/or TotalPrice to make sure that the actual stored value matches the display value. Internally, salesforce.com stores an entire number exactly as it was committed to the database. In other words, it doesn't truncate the data to match the display precision specified on the field, which can cause strange results when using formulas.

2
  • I think you're on to something. I've updated the post with the formula for No_Of_Months__c. It seems like I need to add something to that formula to get it to always use a whole number.
    – logeyg
    Jan 21, 2016 at 18:00
  • 1
    Try adding a FLOOR to the beginning of the formula. Your formula is definitely subject to rounding errors (division), so rounding seems appropriate here. If you need half-rounding, add 0.5 to the value before using FLOOR (e.g. FLOOR(... + 0.5)).
    – sfdcfox
    Jan 21, 2016 at 18:13

You must log in to answer this question.

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