What role do Opportunity Line Items play in managing product-driven sales deals?
Answer
Basics: An opportunity alone has just an Amount. Line items give detail: what is being sold, how many, at what price. That drives product-level reporting, quantity and revenue schedules, and forecasting by product family.
How it works: 1) Products (Product2) are created and given prices in the Standard Price Book via PricebookEntry. 2) Custom price books can add entries for other markets or customers. 3) An opportunity is linked to one price book; adding the first product sets it. 4) A line item stores OpportunityId, PricebookEntryId, Quantity, UnitPrice or TotalPrice, Discount (percent), ServiceDate and Description. 5) Total Price equals quantity times unit price less discount. 6) Once products exist, the opportunity Amount is calculated from them and is read-only in the UI.
Advanced / deep dive: Every product must exist in the Standard Price Book before it can be in a custom one. Changing the opportunity price book deletes existing line items (with a warning), so integrations must set the price book before inserting items. In multi-currency orgs the price book entry currency must match the opportunity currency. An opportunity has a documented maximum number of products (commonly 200; verify). Product Family supports forecasting and reports. For quotes, quote line items sync with opportunity products. At scale, use Salesforce CPQ or Revenue Cloud for bundles, tiered pricing and approval. On inserts do not populate both UnitPrice and TotalPrice. In tests, use Test.getStandardPricebookId() instead of querying price books.
Code / config example:
// Add one product to many opportunities in bulk
Map<Id, PricebookEntry> pbeByProduct = new Map<Id, PricebookEntry>();
for (PricebookEntry p : [SELECT Id, Product2Id, UnitPrice FROM PricebookEntry
:pricebookId :productId
:isoCode]) {
pbeByProduct.put(p.Product2Id, p);
}
List<OpportunityLineItem> items = <OpportunityLineItem>();
(Opportunity o : opps) {
pbeByProduct.get(productId);
(pbe != ) {
items.add( (OpportunityId = o.Id, PricebookEntryId = pbe.Id,
Quantity = , UnitPrice = pbe.UnitPrice));
}
}
insert items;