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.

12
  • 1
    how are you running this code? Within a trigger, anonymous apex, through a soap call, etc. Commented Dec 31, 2019 at 13:20
  • anonymous apex. Commented Dec 31, 2019 at 13:22
  • What do the values of this OpportunityLineItem look like? In particular, does it already have values for quantity, discount, and TotalPrice? Commented Dec 31, 2019 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
    Commented Dec 31, 2019 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? Commented Dec 31, 2019 at 13:40

1 Answer 1

5

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.

1

You must log in to answer this question.

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