19

When writing test cases with SeeAllData=false, you cannot access or insert Pricebooks.

Effectively this means that you cannot use SeeAllData=false when working with any test case involving products.

Salesforce indicated this was a known bug when this feature was first rolled out and were working on a fix, however I've yet to see any resolution to this.

Does anybody know if there's a workaround currently?

1
  • see Mohith's answer which is valid as of V31
    – cropredy
    Commented Aug 14, 2014 at 19:45

4 Answers 4

6

There is still no resolution to this - the pre-release Summer 13 Apex docs still say to use seeAllData=true in this case:

http://www.salesforce.com/us/developer/docs/apexcodepre/Content/apex_testing_data_access.htm?SearchType=Stem&Highlight=SeeAllData

24

Starting summer 14, you can now use Test.getStandardPricebookId() to get the Id of the current Pricebook. This works in Test classes only.

Here is a sample on how this works in practice

    @isTest
    public class PriceBookTest {
        // Utility method that can be called by Apex tests to create price book entries.
        static testmethod void addPricebookEntries() {
            // First, set up test price book entries.
            // Insert a test product.
            Product2 prod = new Product2(Name = 'Laptop X200', Family = 'Hardware');
            insert prod;
            // Get standard price book ID.
            // This is available irrespective of the state of SeeAllData.
            Id pricebookId = Test.getStandardPricebookId();
            // 1. Insert a price book entry for the standard price book.
            // Standard price book entries require the standard price book ID we got earlier.
            PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true);
            insert standardPrice;
            // Create a custom price book
            Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
            insert customPB;
            // 2. Insert a price book entry with a custom price.
            PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id, UnitPrice = 12000, IsActive = true);
            insert customPrice;
            // Next, perform some tests with your test price book entries.
         }
    }
2
  • I wish you would've highlighted the new Test.getStandardPricebookId() function as the important key feature. I didn't see that at first and kept looking elsewhere for a solution. Commented Aug 14, 2014 at 19:48
  • What if the standard price book is deactivated.
    – Eric
    Commented Aug 14, 2014 at 20:01
6

There's finally a solution that doesn't use SeeAllData!

Use Test.getStandardPricebookId() to get the ID of the Standard Pricebook from within your test methods.

http://blog.force365.com/2014/05/22/salesforce-summer-14-platform-highlights/

edit: Oh, Mohith hinted at that.

3

I believe you can, but keep in mind that products need to be inserted in the Standard Pricebook before they can be inserted in a custom one.

After that, the annotation @isTest(seeAllData=true) will grant test classes and individual test methods access to all data in the organization, including pre-existing data that the test didn’t create (like entries in the Standard Pricebook).

1
  • 1
    Yeah, PriceBooks require you use seeAllData=true... it's annoying but put those bits in their own test class and you can keep it tidy.
    – Matt Lacey
    Commented Apr 26, 2013 at 1:28

You must log in to answer this question.

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