Thursday 22 August 2013

Understanding PriceBook, Product, and PricebookEntry Relationships

In the API:
  • Pricebooks are represented by Pricebook2 objects.
  • Products are represented by Product2 objects.
  • 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.


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.

PricebookEntry

  • Use this object to define the association between your organization’s products (Product2) and your organization’s standard price book or to other, custom-defined price books ( Pricebook2).
  • Create one PricebookEntry record for each standard or custom price and currency combination for a product in a Pricebook2.
  • When creating these records, you must specify the IDs of the associated Pricebook2 object and Product2 object.
  • You must load the standard price for a product before you are permitted to load its custom price(s).


Price Book Setup


The process of setting up a price book via the API usually means:

  1. Initially loading product data from your organization into Product2 objects (creating a Product2 record for each product that you want to add).
  2. 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.
  3. Creating a custom Pricebook2.
  4. Querying the Pricebook2 object to obtain their IDs.
  5. 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.


Important Note:

  • PricebookEntry is a junction object between Pricebook2 and Product2.
  • OpportunityLineItem is a junction object between PricebookEntry and Opportunity.

Code Sample—Java


public void pricebookSample() {
  try {
    //Create a custom pricebook
    Pricebook2 pb = new Pricebook2();
    pb.setName("Custom Pricebok");
    pb.setIsActive(true);
    SaveResult[] saveResults = connection.create(new SObject[]{pb});
    pb.setId(saveResults[0].getId());

    // Create a new product
    Product2 product = new Product2();
    product.setIsActive(true);
    product.setName("Product");
    saveResults = connection.create(new SObject[]{product});
    product.setId(saveResults[0].getId());
    
    // Add product to standard pricebook
    QueryResult result = connection.query(
        "select Id from Pricebook2 where isStandard=true"
    );
    SObject[] records = result.getRecords();
    String stdPbId = records[0].getId();
    
    // Create a pricebook entry for standard pricebook
    PricebookEntry pbe = new PricebookEntry();
    pbe.setPricebook2Id(stdPbId);
    pbe.setProduct2Id(product.getId());
    pbe.setIsActive(true);
    pbe.setUnitPrice(100.0);
    saveResults = connection.create(new SObject[]{pbe});
    
    // Create a pricebook entry for custom pricebook
    pbe = new PricebookEntry();
    pbe.setPricebook2Id(pb.getId());
    pbe.setProduct2Id(product.getId());
    pbe.setIsActive(true);
    pbe.setUnitPrice(100.0);
    saveResults = connection.create(new SObject[]{pbe});
  } catch (ConnectionException ce) {
    ce.printStackTrace();
  }
}

3 comments:

  1. your method doesn't work in c#.net why?

    ReplyDelete
    Replies
    1. This will work in Java and Apex. I don't have idea about c# .net.

      Thanks!

      Delete
  2. don't you need to write UseStandardPrice=false while creating cutom pricebookentry

    ReplyDelete

Thank you for visiting. Your comments are highly appreciated.