Resolving UnitPrice Recalculation for OpportunityLineItem Update
Tags: salesforce, opportunitylineitem, unitprice, opportunityproduct
Published: 2023-06-06 07:38:11

In Salesforce, the Opportunity Product standard object (OpportunityLineItem) serves as the foundation for many functionalities. While custom objects and fields generally exhibit consistent behavior, standard objects can have more complex logic with additional dependencies and limitations. 

Recently, I encountered an unusual behavior related to the OpportunityLineItem's UnitPrice field recalculation. This recalculation occurs when a record is updated indirectly, outside of the Salesforce Lightning or Classic record pages. It may happen through updates made via Apex, Automation Tools, or Extensions (such as Salesforce Inspector or similar tools).

The behavior specifically revolves around the Quantity and UnitPrice fields, and how the UnitPrice is recalculated when the Quantity is modified without explicitly querying the UnitPrice field. In this article, I will share my observations of this behavior and provide a workaround to ensure accurate updates while preserving the original UnitPrice value.

##Example

Let's examine a scenario to better understand this behavior:


![Initial values](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsfjzbix5na74swa3201.png)

Consider an Opportunity Product record with the following initial data:

* Sales Price (API name UnitPrice): 100,000 EUR
* Total Price: 100,000 EUR
* Quantity: 1

We will perform updates on the Quantity field, setting it to a value of '2', and observe the outcomes in different scenarios.

### Update from the Lightning Record Page


![lightning update](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gelr47ob5dwnbomvbyp.gif)

In this case, the behavior is as expected. The Quantity is updated to '2', the Sales Price remains unchanged, and the Total Price becomes 200,000 EUR, which is logically consistent.


![After lightning update](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c6xu9im5tm987al36bq0.png)

### Update from Apex

Let's update this record using Anonymous Apex with the following code snippet:

```
OpportunityLineItem oli = [SELECT Id, Quantity FROM OpportunityLineItem WHERE Id = '00k6800000c3THrAAM']; //Id of selected OLi
oli.Quantity = 2;
update oli;
```

![Apex update no UnitPrice](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1ceifeilo7un6e0ya3n.gif)

In this scenario, we observe that the Total Price remains the same, but the Sales Price becomes two times smaller, like following the formula Total Price/Quantity.

## Why UnitPrice gets recalculated

Upon investigating this unusual behavior, I discovered that these fields are interdependent based on the following formula:
`TotalPrice = (UnitPrice * Quantity) * Discount`

When only the Quantity field is updated without querying the UnitPrice field, the system recalculates the UnitPrice as TotalPrice divided by (Quantity * Discount).

## Workaround

To overcome this unexpected UnitPrice recalculation, I found a workaround that involves explicitly querying the UnitPrice field. By doing so, we can retain the original UnitPrice value while updating the Quantity accurately.

Here's an example implementation, along with a video demonstration:

```
OpportunityLineItem oli = [SELECT Id, UnitPrice, Quantity FROM OpportunityLineItem WHERE Id = '00k6800000c3THrAAM'];
oli.Quantity = 2;
update oli;
```

![Quantity update with UnitPrice](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmtvse2jz0bj8u5dgqmm.gif)



As you can see, even without directly updating the UnitPrice field, just by adding it to the query, we achieve the expected behavior and avoid unnecessary recalculations.

If you have any alternative workarounds in mind or are aware of other action sequences where field recalculation may occur, please feel free to share them in the comments section. Your insights and contributions are greatly appreciated!