function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bohemianguy100bohemianguy100 

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.

 

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

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:

 

public static testMethod void test3() 
    { 
    	Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];

        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 standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert standardPrice;

    	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];
    } 

 

 

All Answers

Ankit AroraAnkit Arora

Hi,

 

To insert a pricebook you need a standard pricebook which exists on every org.

 

So you can try something like this :

 

 

//create pricebook
Pricebook2 pbook = [select id from Pricebook2 where IsStandard = true limit 1];

//create pricebook entry
PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pbook.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
insert pbe;

 

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

 

forecast_is_cloudyforecast_is_cloudy

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:

 

public static testMethod void test3() 
    { 
    	Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];

        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 standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert standardPrice;

    	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];
    } 

 

 

This was selected as the best answer
Steve CapSteve Cap

Excellent. Thank you.

bprakashbprakash

did this solved your issue?

i did the same thing but for standard price book its not returning anything

Thanks

Bhanu

sfdcdev.wordpress.comsfdcdev.wordpress.com
TonyScottTonyScott

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. :-(

 

 

DharmikDharmik
Excellent sollution here. but after using this solution i got this error like 'Entity Pricebook not  accessible in version 29.0'  is there any solution for that.i can't change my version because i need to use seeAllData so later version not support seeAllData. Thank in advance for your solution.
Jason HardyJason Hardy
There is new functionality in Summer 14 that resolves this issue. Use this: ID standardPBID = Test.getStandardPricebookId(); and you will not have use see all data.