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

unit test error - standard price not defined
I'm writing a unit test on the opportunity and opportunity line item. I am getting the error "No standard price is defined".
Here is my test method:
public static testMethod void test3() { Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true); insert pb; Product2 prod = new Product2(Name = 'Anti-infectives 2007', Family = 'Best Practices', Practice__c = 'GP/BP', Service_Level__c = 'Partial Service', Product_Abbrev__c = 'CMR_Antiinfect', Sales_Unit__c = 'Each', Standard_Unit__c = 'Each', Product_Year__c = '07', Item_Type__c = 'Non-Inventory (Sales only)', IsActive = true); insert prod; PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false); insert pbe; Opportunity opp = new Opportunity(Name = 'Test Syndicated 2010', Type = 'Syndicated - New', StageName = 'Planning', CloseDate = system.today()); insert opp; OpportunityLineItem oli = new OpportunityLineItem(opportunityId = opp.Id, pricebookentryId = pbe.Id, Quantity = 1, UnitPrice = 7500, Description = '2007 CMR #4 - Anti-Infectives'); insert oli; List<OpportunityLineItem> olis = [Select Id From OpportunityLineItem Where OpportunityId =: opp.Id]; update olis[0]; }
The error is thrown on the line where I try and insert the PricebookEntry record:
PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false); insert pbe;
I'm not sure what I'm missing.
Thanks for any help.
Regards.
Every Product has to have a Standard Price that is part of the Standard Price Book before you can create any additional prices in other Price Books. So changing your code thus should do the trick:
All Answers
Hi,
To insert a pricebook you need a standard pricebook which exists on every org.
So you can try something like this :
Hopefully this will resolve your problem but you have to find out way how you can test you functionality using custom pricebook.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Every Product has to have a Standard Price that is part of the Standard Price Book before you can create any additional prices in other Price Books. So changing your code thus should do the trick:
Excellent. Thank you.
did this solved your issue?
i did the same thing but for standard price book its not returning anything
Thanks
Bhanu
http://stackoverflow.com/questions/9164986/how-do-i-avoid-standard-price-not-defined-when-unit-testing-an-opportunitylineit
This started with Apex 24 upgrade: here is the solution:
http://sfdcdev.wordpress.com/2012/02/18/standard-price-book-not-found-in-apex-test-methods/
Unfortunately this is not a solution if you want to take advantage of Spring 12's feature of being able to isolate test data from org data if your test requires products and pricebooks. :-(