function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Adam CadamAdam Cadam 

Opportunity Line Item UnitPrice Sometimes not updating in BeforeInsert trigger

0
In my BeforeInsert trigger for OpprtunityLineItemTrigger, I check if the product requires VAT (sales tax in the US) and add it to the UnitPrice if it does.
This works most of the time, but sometimes fails.
Here's the code that does this:
boolean vatableEntry = entry.Vatable_Entry__c;
Decimal entryFee = (entry.Registration_Fee_Calculation__c <> NULL ? entry.Registration_Fee_Calculation__c : 0);
// If VAT Applicable, add it to the Entry Registration fee
oli.Vatable_Entry__c = vatableEntry ;
if (vatableEntry){
    Decimal vat = (entryFee * (entry.Event__r.VAT_Percentage__c/100)).setScale(2);
    oli.Cost_Comment__c = '(pre-I) OLI - VATable: ' + entryFee + vat;
    oli.UnitPrice = entryFee + vat;
    system.debug('#### fixEntryOppUnitPrice (pre-I) OLI - VATable: ' + entryFee + vat);
}
else {
    oli.Cost_Comment__c = '(pre-I) OLI - Not VATable: ' + entryFee;
    oli.UnitPrice = entryFee;
    system.debug('#### fixEntryOppUnitPrice (pre-I) OLI - Not VATable: ' + entryFee);
}
And here are two OpportunityLineItems, from the same Opportunity, for the same product:
Correct valuesIncorrect values
In the first image, the price is correct - £87.48 In the 'Cost Comment' field, added in before insert (pre-I) you can see the string from the code: 72.90 entryFee and 14.58 vat

In the second one, processed in just the same way - with the same Cost Comment (both of them say it is Vatable), but the price on the OLI is just £72.90 - the vat has not been added.
This works most of the time (I've not extracted the data to give detailed stats, but probably < than 1 in 100 OLI's).

You can also see that the 'Vatable Entry' flag is set for both OLI's.

So, it's really difficult to reliably reproduce to debug in a sandbox, and the fact it works most of the time means the code is correct. We can also see that the OLI gets updated - the Cost_Comment field is populated in both cases.

Please note - the £0.00 List Price is not part of this. The price for this product, an entry into a competition, doesn't come from a PriceBook but is calculated for each entry - from the Entry__c.Registration_Fee_Calculation__c field - and is the same in both cases.

Any ideas why this might be happening?

 
ShirishaShirisha (Salesforce Developers) 
Hi Adam,

Greetings!

Seems like this is data issue since it is not occuring all the time but on specific records.Also,I understood that you can't replicate the issue in Sandbox so I would suggest you to compare the records which are updated as expected to see the data difference on the records.

Also,please check the dependent classes for the trigger to see,if any other background operations are causing any issue.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

 
Adam CadamAdam Cadam
Shirisha,

Thanks for responding. I'll have another look through the rest of the code for the trigger, but so far, I''ve not found anywhere that restes the UnitPrice to the origianl value.
As to data differences, there are only a few fields used in that bit of code, and all appear to be exactly the same on transactions that fail and those that succeed. You can even see the fields being used in the Cost Comment field - both examples take the some route through the code (vatableEntry == true) and the values used to set the UnitPrice are the same.

Another idea I had was to turn on History tracking for the UnitPrice field on the OpportunityLineItem object - but it doesn't appaer that OLI supports History tracking - Opportunity does though.
If this was more common, System.Debug lines would give some info (and there are some in the code - I left them out for clarity) but the problem is catching those logs. Turning on logging for the account used by the website generates a LOT of logs and by the time we spot an instance of this, it's hard to find the log - even if it still exists.

Regards
Adam