You need to sign in to do that
Don't have an account?
Discrepancy between OpportunityLineItem ListPrice values
Hello All,
I have an OpportunityLineItem (oli) before insert trigger that references the oli.ListPrice value. The trigger works great in my sandbox when manually testing through the UI but is failing due to a null value for oli.ListPrice un unit testing.
I have verified that the issue is specifically that the oli.ListPrice value is null when adding the oli through the unit test code, but the ListPrice is not null when adding an oli through the UI.
Naturally, I suspected the unit test code had an issue with the PriceBook and PriceBookEntry process. But after scouring the code, the Apex Code Language Reference section "Testing the Sample Application", and the developer boards, I'm not seeing anything that explains why the oli.ListPrice value would be null in the unit test.
The behavior seems like the assignment of the oli.ListPrice from the pbe.UnitPrice isn't happening in the correct sequence when the unit test is running but is occurring correctly when the UI is used.
Here is the unit test code that is resulting in a null oli.ListPrice when the oli before insert trigger is firing:
public class testTrigger {
static testMethod void testTriggers_Trigger () {
Product2 p = new product2(name='Test Product');
insert p;
Pricebook2 stdPb = [select Id from Pricebook2 where isStandard=true limit 1];
insert new PricebookEntry(pricebook2id = stdPb.id, product2id = p.id, unitprice=100.0, isActive=true);
Pricebook2 pb = new pricebook2(name='test pricebook');
insert pb;
PricebookEntry pbe = new PricebookEntry(pricebook2id=pb.id, product2id=p.id, unitprice=100.0, isActive=true);
insert pbe;
Opportunity o = new Opportunity(name='test', pricebook2id=pb.id, stageName='Open', CloseDate=Date.newInstance(2009,10,10) );
insert o;
OpportunityLineItem oli = new OpportunityLineItem(opportunityid=o.id, UnitPrice=100, quantity=1, pricebookentryid=pbe.Id);
insert oli;
}
}
I hope that you read this post and chuckle as you gently explain the obvious piece that I'm missing. Otherwise, any input is appreciated.
Cheers,
-Philip
I've found the solution now. Some "magical" fields aren't automatically given a value when creating objects in Apex. This includes ListPrice and Product2, for instance. The solution is to refer to them through the PricebookEntry object.
I used this construct:
Decimal listPrice = [select UnitPrice from PriceBookEntry where Id = :opl.PricebookEntryId].UnitPrice;
(where opl is my OpportunityLineItem).
HTH,
Hans
Did you got any work around and trick for this issue>