4

I'm on the advanced apex super badge step 7, and before this I thought I had a good understanding of order/orderItem/pricebookEntry/product2/pricebook2 objects.

However, upon creating my own relationship/data model of the objects, I'm getting a bit confused about when to use each object, e.g. both product2 and pricebookEntry have IsActive fields.

When reading about how to implement the standard set controller, everyone seems to recommend pricebookEntry to create the orderitem list for pagination. Why can't we use Product2 directly as its still supported in standardsetcontroller?

Also UnitPrice field is available on both OrderItem and PricebookEntry. Which do I use if I want to show and ammend the field as per the superbadge?

And my main concern is if I'm creating a new order or editing an order's items, do I need to create PricebookEntry each time?

Is there anything else I'm missing about these objects, particularly if it relates to the superbadge?

Thank you.

5

It's helpful to understand the various concepts directly.

Product (Product2)

Represents a product. A product is something that can be sold. For example, you walk in to a store and see packages of pencils; each package has a SKU or UPC that would be considered a Product Code. Such a product can be active or not (can be sold or not).

Price Book (Pricebook2)

Represents a price book. A price book is simply a collection of products to be sold to a particular group of people. For example, you might have a Military Discount price book, a Government price book, a United States price book, etc. Each price book can either be active (e.g. can be used in new opportunities) or not.

Price Book Entry (PricebookEntry)

Represents a product's specific price, in a specific currency, in a specific price book. Each price can either be active (can be added to new opportunities, orders, etc) or not. This is what we call a "junction object," as it joins two objects together. Many products can exist in a price book, and a product can exist in many price books.

Order (Order)

Represents an order to purchase products (among other uses of the Order object). This is a top level collection that represents an entire order. For example, if you want to order some pencils, some paper, a few erasers, and a binder, this might all be one order. An Order always pulls from a single Price Book.

Order Lines (OrderLineItem)

Represents a product that will be purchased as part of the order. It's attached to an Order, Product, Price Book, and Price Book Entry. The various sundries mentioned in the previous paragraph would each have a line item for that order. You specify how many you want to buy, what the price is, if there's any discounts, etc.


With all of that out of the way, you need to create things from the top down. Price Books and Products must be first (order not important), then Price Book Entries, then Orders, then Order Line Items.

Unit Price on the Price Book Entry is what we call a list price. When you call a place or check our their web site, the price that you see there is the list price, the normal price that someone would pay for that product. Changing this value will affect all future orders that use that product for that price book in that currency.

Unit Price on the Order Line Item defaults from the Price Book Entry, but can be manually adjusted. We call this the sales price, which is a (possibly adjusted) amount that this particular customer will pay for the product being ordered. This might be because of a coupon, volume purchasing, part of a deal to secure the order, or whatever else might cause the price to be adjusted. Changing this value does not affect the list price from above.

Price Book Entries have a unique constraint, in that you cannot add the same product in the same currency to the same price book. You do not need to create new Price Book Entries every time you want to use a product on an opportunity or order.


When reading about how to implement the standard set controller, everyone seems to recommend pricebookEntry to create the orderitem list for pagination. Why can't we use Product2 directly as its still supported in standardsetcontroller?

Because you don't know which price to use. Remember, a product can exist in many different price books, and depending on which price book is selected, you won't necessarily be able to select all possible products. Besides, you need the Price Book Entry's ID to successfully create the Order Line Item.

Also UnitPrice field is available on both OrderItem and PricebookEntry. Which do I use if I want to show and ammend the field as per the superbadge?

If you're editing an Order's Line Items, you want to modify the Unit Price for those line items, the sales price. If you want to modify the price for all future orders, you modify the Price Book Entry.

And my main concern is if I'm creating a new order or editing an order's items, do I need to create PricebookEntry each time?

As covered before, you cannot do so. Each unique combination of Product2, Pricebook2, and CurrencyIsoCode can only exist once in the database. Again, if you're creating new Orders, you're also creating new Order Line Items.

Is there anything else I'm missing about these objects, particularly if it relates to the superbadge?

Hopefully, I've already done a good enough job of explaining, but you might also look at the Sales Objects ERD (Entity Relationship Diagram), which has a nice chart that lays out the various relationships graphically. If you're still not sure, you might also play with the Schema Builder, which lets you select the individual objects you want and rearrange them as you like.

  • Thank you so much for this perfect answer. I already created my own ERD based off developer.salesforce.com/docs/atlas.en-us.api.meta/api/… and the schema builder to show all fields and relationships for these objects. However, your reply made me understand it far better than any documentation. Thanks again. – rna7 Jun 21 '18 at 10:15
  • Quick question. What is the ListPrice field on OrderItem and when do we use that rather than UnitPrice? – rna7 Jun 21 '18 at 10:45
  • 1
    @rna7 List Price on Order Line Item is the Price Book Entry's Unit Price. Typically, you use this to show the difference between what the standard price is versus what the order was billed at (the Order Line Item's unit price). You can divide the two together to determine a Discount % (1 - List Price / Unit Price). The important value for the Order Line Item is the Unit Price, since that determines how much the order will actually be billed at. – sfdcfox Jun 21 '18 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.