Cannot change both ‘UnitPrice’ and ‘TotalPrice’ in update call

Trigger to change UnitPrice in Salesforce

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 :

  1. Use Process builder to compute Unit Price as per formula. Process builder field update works after trigger so it went through.
  2. In after update trigger, create list of Opportunity Product to be updates and make explicit update call for all records which qualified for update.

Related posts

One thought on “Cannot change both ‘UnitPrice’ and ‘TotalPrice’ in update call”

Leave a Reply to Venkata Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.