Products:
Products are the actual items that you sell on your opportunities and quotes. Products are associated to one or more price books to create price book entries. Products can exist in multiple price books with many different prices.
For example, you may have a "North India" price book with one set of prices for your products and a "South India" price book with a different set of prices.
Price Books:
A price book is a list of products and their associated prices. Each product and its price is called a price book entry.
Salesforce provides two types of price books—standard and custom.
The standard price book is a master list of all products with their associated default or standard prices. It automatically lists all products and standard prices regardless of the custom price books that also contain them.
A custom price book is a list of products with their custom or list prices, making them ideal for offering different prices to different market segments. Custom price books can contain discounted list prices or list prices that are higher than the standard price.
Price Book Entries:
A price book entry is a price that’s defined for a product and price book. Salesforce provides two types of price book entries—standard and custom. Standard price book entries represent the default or standard prices for the products and services that are sold. Custom price book entries represent the custom or list prices for the products and services that are sold. A custom price book entry can be created in a custom price book only if an active standard price book entry exists for that product or service in the standard price book.
Notes:
- The API name of Product is Product2.
- The API name of price books is Pricebook2.
- The API Name of Price book Entry is PricebookEntry.
- You can create up to 50 Price Book Entry custom fields.
- PricebookEntry is a junction object between Pricebook2 and Product2.
- OpportunityLineItem is a junction object between PricebookEntry and Opportunit.
- We can write the trigger on Product2 and PriceBook2 but we cannot write the trigger on PricebookEntry2 because it s a junction object and we cannot write trigger on Junction Object.
Create Price Book Entries in Apex Tests:
You can create price book entries for standard and custom price books in Apex tests.
Previously, you couldn’t create price book entries in an Apex test by default unless the test had access to organization data via the @isTest(SeeAllData=true) annotation. With this new support, you can isolate your price book test data from your organization data. Note that custom price books can be created but standard price books cannot.
Support for test price book entries is added for all tests, including tests that use the default data isolation mode (tests that can’t access organization data).
With this support, you can do the following.
- Query for the ID of the standard price book in your organization with the Test.getStandardPricebookId() method.
- Create test price book entries with standard prices by using the standard price book ID that’s returned by Test.getStandardPricebookId().
- Create test custom price books, which enables you to add price book entries with custom prices.
@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.
}
}
Price Book Entries:
A price book entry is a price that’s defined for a product and price book. Salesforce provides two types of price book entries—standard and custom. Standard price book entries represent the default or standard prices for the products and services that are sold. Custom price book entries represent the custom or list prices for the products and services that are sold. A custom price book entry can be created in a custom price book only if an active standard price book entry exists for that product or service in the standard price book.
Notes:
- The API name of Product is Product2.
- The API name of price books is Pricebook2.
- The API Name of Price book Entry is PricebookEntry.
- You can create up to 50 Price Book Entry custom fields.
- PricebookEntry is a junction object between Pricebook2 and Product2.
- OpportunityLineItem is a junction object between PricebookEntry and Opportunit.
- We can write the trigger on Product2 and PriceBook2 but we cannot write the trigger on PricebookEntry2 because it s a junction object and we cannot write trigger on Junction Object.
You can create price book entries for standard and custom price books in Apex tests.
Previously, you couldn’t create price book entries in an Apex test by default unless the test had access to organization data via the @isTest(SeeAllData=true) annotation. With this new support, you can isolate your price book test data from your organization data. Note that custom price books can be created but standard price books cannot.
Support for test price book entries is added for all tests, including tests that use the default data isolation mode (tests that can’t access organization data).
With this support, you can do the following.
- Query for the ID of the standard price book in your organization with the Test.getStandardPricebookId() method.
- Create test price book entries with standard prices by using the standard price book ID that’s returned by Test.getStandardPricebookId().
- Create test custom price books, which enables you to add price book entries with custom prices.
@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.
}
}
This comment has been removed by the author.
ReplyDelete