DEV Community

Cover image for Resolving UnitPrice Recalculation for OpportunityLineItem Update
Oleksandra Todorenko
Oleksandra Todorenko

Posted on

Resolving UnitPrice Recalculation for OpportunityLineItem Update

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

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

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

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;
Enter fullscreen mode Exit fullscreen mode

Apex update no UnitPrice

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;
Enter fullscreen mode Exit fullscreen mode

Quantity update with UnitPrice

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!

Top comments (0)