[SalesForce] Trigger for calculation of UnitPrice in OppLineItem

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.

Best Answer

  • 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.

Related Topic