I have the following scenario:

I need to calculate the UnitPrice of OppLineItem based on a calculation that takes into account other parameters, such as custom fields and formulas created for the OppLineItem.

This calculation will be done via a method that is called on the onBeforeInsert/Update events of theOli trigger.

Now the curious part of the story is that when I created the corresponding Test method and started executing it, it has failed on every trial . I debugged the code, and in the debug log I was obtaining the desired value on UnitPrice, nonetheless, in the System.assert call on my Test, the assertion always fails , and always outputs as the value of UnitPrice, the same value that is assigned to the PricebookEntry's UnitPrice, the PricebookEntry that corresponds to the Oli I am testing.

I did a little research on the documentation and I found this :

Description for ListPrice of Oli: Corresponds to the UnitPrice on the PricebookEntry that is associated with this line item, which can be in the standard pricebook or a custom pricebook. A client application can use this information to show whether the unit price (or sales price) of the line item differs from the pricebook entry list price.

I didn't found anywhere in the documentation an explicit relation between the UnitPrice of Oli and the Unit Price of its corresponding PBE.

Am I missing something here?

I think that my problem is somehow theoretical , in terms of concepts around the functionality and relations between PBE and OLI. Nonetheless I could also provide the code if needed, let me know!

Thanks a lot.

  • You can only specify either of TotalPrice or UnitPrice, not both. Are you sure you set just one value?
    – sfdcfox
    Commented Oct 30, 2015 at 17:45
  • @sfdcfox Yes, I have double checked this , I am setting only the UnitPrice inside the method that is launched in the Trigger, as I mentioned above, assigning it the value of the formula.
    – Menio
    Commented Nov 2, 2015 at 8:15

2 Answers 2

  • List Price(api:UnitPrice) of PriceBookEntry will always be the same
    as per the setup done at PBE level.
  • List Price(api:ListPrice) of OpportunityLineItem will be same as List Price of PBE
  • Sales Price(api:UnitPrice) of OpportunityLineItem can be entered by user when he is adding OLI's to Opportunity. So this can be calculated by your complex formulae.And this is probably what you ought to check in test class.
  • Total Price(api:TotalPrice) of OpportunityLineItem is auto calculated by system as Sales Price of OLI * Quantity of OLI. This is always read-only for users

Hope this provides clarity.


If you're just trying to calculate the UnitPrice, can't you just use a Workflow Rule? I was actually looking into this same thing today, and that's what I came up with and it works very well.

Rule Name: Update Sales Price

Object: Opportunity Product

Rule Criteria: Opportunity Product: Quantity NOT EQUAL TO null

Immediate Workflow Action: Field Update:

Name: Calculate Sell Price
Unique Name:Calculate_Sell_Price
Object: Opportunity Product
Field to Update: Opportunity Product: Sales Price

Formula Value: Total_Value__c / Quantity

My formula is a simple calculation of the total amount and the quantity to get the UnitPrice. But I'm sure you can modify it to be whatever you need. Once the line item is saved, the UnitPrice updates.

You must log in to answer this question.

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