4

I was hopping to be able to remove the @seeAllData annotation from my tests by using Summer'14's addition - Test.getStandardPricebookId() but the test are failing with this error:

System.DmlException: Update failed. First exception on row 0 with id 0064E000003zx4hQAA; first error: INVALID_OPERATION, This quote can't be synced because it has an inactive or archived price book.: []

The error occurs because the test's set up is trying to set a quote created with the pricebook id obtained from the method as a synced quote. Am I doomed to revert to my seeAlldata version?


I can't put code in comments. I have 2K lines of test class that test syncing custom fields on quote/quote lines and opportunity/opportunity lines.
This is more or less how each test looks like

IdstdPriceBookId = Test.getStandardPricebookId();

Opportunity opp = new Opportunity(Name='test', TotalOpportunityQuantity=10, StageName='Hot',  CloseDate=System.today(), Pricebook2Id=stdPriceBookId, AccountId=acc.Id);
Quote quote = new Quote(Name='test', OpportunityId=opp.Id, Pricebook2Id=opp.Pricebook2Id);

private static void startSync(String oppId, String quoteId) { 
        Opportunity opp = [SELECT SyncedQuoteId FROM Opportunity where Id = :oppId];
        opp.SyncedQuoteId = quoteId;
        update(opp); <--error trhown  
}
2
  • 2
    can you put your main class for which you are testing Commented Apr 27, 2017 at 21:38
  • You can always edit your own posts.
    – Adrian Larson
    Commented May 1, 2017 at 17:28

1 Answer 1

4

Create a custom price book, set is as active (IsActive=true) and use it. If you are using a price book entry, then make sure standard entry is created before the custom one. This is because a standard price is required to create a custom entry

    Product2 seedProduct = new Product2(name='Product',productCode='XX1234', isActive=true);
    insert seedProduct;

    id standardPBID = Test.getStandardPricebookId(); 

    PricebookEntry stdPBE = new PriceBookEntry(
        Product2Id=seedProduct.Id,
        Pricebook2Id=standardPBID,
        UnitPrice = 3000000.00,
        UseStandardPrice = false,
        IsActive=true
    );

    insert stdPBE;

    PriceBook2 customPriceBook = new PriceBook2();
    customPriceBook.Name='Custom Pricebook';
    customPriceBook.IsActive=true;
    insert customPriceBook;

    PricebookEntry cusPBE = new PriceBookEntry(
        Product2Id=seedProduct.Id,
        Pricebook2Id=customPriceBook.Id,
        UnitPrice = 5000000.00,
        UseStandardPrice = false,
        IsActive=true
    );

    insert cusPBE;
1
  • You can actually build a list of two PBE as long as the standard price book is list element 0, then only one bulkified DML insert is needed.
    – cropredy
    Commented May 16, 2019 at 5:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .