1

I am trying to update the UnitPrice on the OpportunityLineItem object; however, it does not update the field, my code is as follows:

Id oliId = '00k1w000006SpWE';

OpportunityLineItem oli = [SELECT Id, UnitPrice FROM OpportunityLineItem WHERE Id = :oliId];
oli.UnitPrice = 100.01;

update oli;

I am expecting the unit price to be 100.01; however, it shows the old value.

I checked the SOAP API for OpportunityLineItem, and it stays that UnitPrice field can be updated. I also disabled the triggers and workflow rules.

  • 1
    how are you running this code? Within a trigger, anonymous apex, through a soap call, etc. – Kris Goncalves Dec 31 '19 at 13:20
  • anonymous apex. – Cuban coffee Dec 31 '19 at 13:22
  • What do the values of this OpportunityLineItem look like? In particular, does it already have values for quantity, discount, and TotalPrice? – Kris Goncalves Dec 31 '19 at 13:29
  • 1
    Are you getting any error message when executing your anon apex? Salesforce should be complaining that UnitPrice and TotalPrice cannot both be set (or maybe that only happens if you query both fields). – Derek F Dec 31 '19 at 13:38
  • 2
    For what it's worth, your code works in my sandbox and reflects the new price in the UI. Does the opp product show yourself as "last modified by" after running the anonymous code? – Kris Goncalves Dec 31 '19 at 13:40
4

I was able to update the UnitPrice by first removing all its related OpportunityLineItemSchedule records, the code as follows:

Id oliId = '00k1w000006SpWE';

List<OpportunityLineItemSchedule> olisList = [SELECT Id FROM OpportunityLineItemSchedule WHERE OpportunityLineItemId = :oliId];

delete olisList;

OpportunityLineItem oli = [SELECT Id, UnitPrice FROM OpportunityLineItem WHERE Id = :oliId];
oli.UnitPrice = 100.01;

update oli;

Now I can update the UnitPrice. I thought I got an issue with some triggers or further checks, for example, workflow rules or process builders; however, after I disabled all of them, I still got the same issue.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.