Newer Version Available
Pricebook2
Supported Calls
create(), delete(), describeLayout(), describeSObjects(), getDeleted(), getUpdated(), query(), retrieve(), search(), undelete(), update(), upsert()
Fields
| Field | Details |
|---|---|
| Description |
|
| IsActive |
|
| IsDeleted |
|
| IsStandard |
|
| Name |
|
Usage
A price book is a list of products that your organization sells:
- Each organization has one standard price book that defines the standard or generic list price for each product or service that it sells.
- An organization can have multiple custom price books that can be used for specialized purposes, such as a discount price book, price books for different channels or markets, price books for select accounts or opportunities, and so on. While your client application can create, delete, and update and custom price books, your client application can only update the standard price book.
- For some organizations, the standard price book might be the only price needed, but if you need to set up further price books, you can reference the standard price book when setting up list prices in custom price books.
Use this object to query standard and custom price books that have been configured for your organization. A common use of this object is to allow your client application to obtain valid Pricebook2 object IDs for use when configuring PricebookEntry records via the API.
Your client application can perform the following tasks on PricebookEntry objects:
- Query
- Create for the standard pricebook or custom pricebooks.
- Update
- Delete
- Change the IsActive field when creating or updating records
PriceBook2, Product2, and PricebookEntry Relationships
In the API:
- Price books are represented by Pricebook2 objects (the Pricebook object is no longer available).
- Products are represented by Product2 objects (the Product object is not available as of version 8.0.).
- Each price book contains zero or more entries (represented by PricebookEntry records) that specify the products that are associated with the price book. A price book entry defines the price for which you sell a product at a particular currency.
These objects are defined only for those organizations that have products enabled as a feature. If the organization does not have the products feature enabled, the Pricebook2 object does not appear in the describeGlobal() call, and you can’t access it via the API.
If you delete a Pricebook2, while a line item references PricebookEntry in the price book, the line item is unaffected, but the Pricebook2 will be archived and unavailable from the API.
For a visual diagram of the relationships between Pricebook2 and other objects, see Product and Schedule Objects.
Price Book Setup
The process of setting up a price book via the API usually means:
- Initially loading product data from your organization into Product2 objects (creating a Product2 record for each product that you want to add).
- For each Product2 object, creating a PricebookEntry that links the Product2 object to the standard Pricebook2. You need to define a standard price for a product at a given currency (if you have multicurrency enabled), before defining a price for that product in the same currency in a custom price book.
- Creating a custom Pricebook2.
- Querying the Pricebook2 object to obtain their IDs.
- For each Pricebook2 object, creating a PricebookEntry for every Product2 that you want to add, specifying unique properties for each PricebookEntry (such as the UnitPrice and CurrencyIsoCode) as needed.
Code Sample—Java
1public void pricebookSample() {
2 try {
3 //Create a custom pricebook
4 Pricebook2 pb = new Pricebook2();
5 pb.setName("Custom Pricebok");
6 pb.setIsActive(true);
7 SaveResult[] saveResults = connection.create(new SObject[]{pb});
8 pb.setId(saveResults[0].getId());
9
10 // Create a new product
11 Product2 product = new Product2();
12 product.setIsActive(true);
13 product.setName("Product");
14 saveResults = connection.create(new SObject[]{product});
15 product.setId(saveResults[0].getId());
16
17 // Add product to standard pricebook
18 QueryResult result = connection.query(
19 "select Id from Pricebook2 where isStandard=true"
20 );
21 SObject[] records = result.getRecords();
22 String stdPbId = records[0].getId();
23
24 // Create a pricebook entry for standard pricebook
25 PricebookEntry pbe = new PricebookEntry();
26 pbe.setPricebook2Id(stdPbId);
27 pbe.setProduct2Id(product.getId());
28 pbe.setIsActive(true);
29 pbe.setUnitPrice(100.0);
30 saveResults = connection.create(new SObject[]{pbe});
31
32 // Create a pricebook entry for custom pricebook
33 pbe = new PricebookEntry();
34 pbe.setPricebook2Id(pb.getId());
35 pbe.setProduct2Id(product.getId());
36 pbe.setIsActive(true);
37 pbe.setUnitPrice(100.0);
38 saveResults = connection.create(new SObject[]{pbe});
39 } catch (ConnectionException ce) {
40 ce.printStackTrace();
41 }
42}