Skip to content

Instantly share code, notes, and snippets.

@Nahumancer
Created September 4, 2017 20:46
Show Gist options
  • Save Nahumancer/42b6d8cf8d7806955b9841bbdd371cac to your computer and use it in GitHub Desktop.
Save Nahumancer/42b6d8cf8d7806955b9841bbdd371cac to your computer and use it in GitHub Desktop.
Utility method to create Product/Pricebook/PricebookEntry combo for tests. Taken from https://releasenotes.docs.salesforce.com/en-us/summer14/release-notes/rn_apex_price_books_in_tests.htm
@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.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment