You need to sign in to do that
Don't have an account?

Testing an Opportunity Line Item trigger
I have written a simple Opportunity trigger to update a field in Opportunity Line Item records when an Opportunity is moved to certain stages. I am now trying to write a test class so I can deploy it to my production server. When I run the test (code is listed below), I get the error:
15:15:27.915 (915680000)|EXCEPTION_THROWN|[17]|System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []
My test data includes creating test records for Pricebook2, Product2 and PricebookEntry, but none of these objects have a field for the Standard Price and I can't find it in any other object.
Does anyone know where the Standard Price is stored? Below is my test class code - I am just trying to get this simple test to run then I'll be adding more test cases.
-----------------------------------------------------------------------
@isTest
private class TestUpdateClosedListPrice {
static testMethod void TestUpdateClosedListPrice() {
Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Stage 0 - Lead Handed Off',CloseDate=Date.today());
insert opp1;
Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1');
insert pbk1;
Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1');
insert prd1;
PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=pbk1.id,UnitPrice=50);
insert pbe1;
OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=opp1.id,PriceBookEntryID=pbe1.id,Closed_List_Price__c=0);
insert lineItem1;
Test.startTest();
opp1.StageName='Stage 8 - Shipped';
update opp1;
System.assertNotEquals(0, lineItem1.Closed_List_Price__c);
Test.stopTest();
}
}
Hi Symonds,
Here you go: (The below one should work)
@isTest(seeAllData=true)
private class TestUpdateClosedListPrice {
static testMethod void TestUpdateClosedListPrice() {
Account acc = new Account(Name = 'Test Account');
insert acc;
//get standard pricebook
Pricebook2 standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
insert pbk1;
Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
insert prd1;
PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
insert pbe1;
Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Stage 0 - Lead Handed Off',CloseDate=Date.today(),Pricebook2Id = pbe1.Pricebook2Id, AccountId = acc.id);
insert opp1;
OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=opp1.id,PriceBookEntryID=pbe1.id, quantity=4,Closed_List_Price__c=0, totalprice=200);
insert lineItem1;
Test.startTest();
opp1.StageName='Stage 8 - Shipped';
update opp1;
System.assertNotEquals(0, lineItem1.Closed_List_Price__c);
Test.stopTest();
}
}
All Answers
Hi,
Try the below code:
@isTest
private class TestUpdateClosedListPrice {
static testMethod void TestUpdateClosedListPrice() {
//get standard pricebook
Pricebook2 standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Stage 0 - Lead Handed Off',CloseDate=Date.today());
insert opp1;
Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1');
insert pbk1;
Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1');
insert prd1;
//before you create custom pricebooks, create standard ones
PricebookEntry stdpbe = new PriceBookEntry(Pricebook2Id=standardPb.id, Product2Id=pbk1.id, UnitPrice=50);
insert stdPbe;
PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=pbk1.id,UnitPrice=50);
insert pbe1;
OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=opp1.id,PriceBookEntryID=pbe1.id,Closed_List_Price__c=0);
insert lineItem1;
Test.startTest();
opp1.StageName='Stage 8 - Shipped';
update opp1;
System.assertNotEquals(0, lineItem1.Closed_List_Price__c);
Test.stopTest();
}
}
Note: Before inserting custom pricebook, we need first load standard pricebook for that product.
Thanks,
JBabu.
Thank you very much for your response. I tried your code, but the Pricebook2 query for the standard price book returns no rows:
Class.TestUpdateClosedListPrice.TestUpdateClosedListPrice: line 8, column 1
16:12:46.615 (615416000)|FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject
Best Regards,
Jeff
Hi Jeff,
At starting line
change "@isTest"
to "@isTest(seeAllData=true)"
Thanks,
JBabu.
Hi,
It's getting further, but fails when doing the PricebookEntrye stdpbe insert:
Class.TestUpdateClosedListPrice.TestUpdateClosedListPrice: line 21, column 1
16:54:38.538 (1538079000)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Product ID: id value of incorrect type: 01sW00000000CROIA2: [Product2Id]
Best Regards,
Jeff
Hi Symonds,
Here you go: (The below one should work)
@isTest(seeAllData=true)
private class TestUpdateClosedListPrice {
static testMethod void TestUpdateClosedListPrice() {
Account acc = new Account(Name = 'Test Account');
insert acc;
//get standard pricebook
Pricebook2 standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
insert pbk1;
Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
insert prd1;
PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
insert pbe1;
Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Stage 0 - Lead Handed Off',CloseDate=Date.today(),Pricebook2Id = pbe1.Pricebook2Id, AccountId = acc.id);
insert opp1;
OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=opp1.id,PriceBookEntryID=pbe1.id, quantity=4,Closed_List_Price__c=0, totalprice=200);
insert lineItem1;
Test.startTest();
opp1.StageName='Stage 8 - Shipped';
update opp1;
System.assertNotEquals(0, lineItem1.Closed_List_Price__c);
Test.stopTest();
}
}
That did it. Thanks again very much for your help!
Best Regards,
Jeff