Recently, I was in need to update Sales Price (Unit Price) for OpportunityLineItem using Trigger. I wrote a small trigger on before insert and before update of OpportunityLineItem to update Sales Price.
It worked fine on insert however on update, even if user clicked on update button without changing any value, we were getting error Cannot change both ‘UnitPrice’ and ‘TotalPrice’ in update call.
Its Standard Salesforce behavior and seems in background platform tries to perform some calculation for Total Price which is Quantity * Sales Price. I was not able to get it working on before update. However, below two approach worked :
- Use Process builder to compute Unit Price as per formula. Process builder field update works after trigger so it went through.
- In after update trigger, create list of Opportunity Product to be updates and make explicit update call for all records which qualified for update.