SeeAllData=false and Pricebooks
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.
}
}
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
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.