I'm trying to write a test class without the "seealldata=true" option and all I have is a custom pricebook, as my standard pricebook has been deactivated.

The relevant part of the test is as follows;

public class Test1  {
    private static testmethod void Test1a(){

        //Id standardPbId = test.getStandardPricebookId();
        Pricebook2 pbk1 = new Pricebook2 (Name='Standard Price Book 1',Description='Test Pricebook Entry 1', isActive=true);
        insert pbk1;   

        Product2 prd1 = new Product2 (Name='Test Product Entry 1', Family='Best Pricebook ever', isActive = true);
        insert prd1;             

        PricebookEntry pbe1 = new PricebookEntry (Pricebook2ID=pbk1.id,Product2ID=prd1.id,UnitPrice=50.0, isActive=true, UseStandardPrice = false);
        insert pbe1;
        Account acc = new Account(Name = 'Test Account', Type = 'Vendor');
        insert acc;

        Opportunity opp1 = new Opportunity (Name='Opp1', AccountId = acc.id);
        insert opp1;

...But the test fails with a "STANDARD_PRICE_NOT_DEFINED" error.

Any ideas?



To be used in custom price books, all standard prices must be added as price book entries to the standard price book.

So you will still need to define a standard price

PricebookEntry sPBE = new PricebookEntry(Pricebook2Id = test.getStandardPricebookId(), Product2Id = prd1, UnitPrice = 1000);
insert standardPBE;

Additionally, although not related, if StandardPriceBook Is not active and you need to use it simply use:

PriceBook2 pb = New PriceBook2(Id = test.getStandardPricebookId(), isActive = true);
update pb;

Which will allow you to use the standard price book.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.