# What role do Opportunity Line Items play in managing product-driven sales deals?

**Topic:** Sales Cloud · **Difficulty:** Medium · **Tags:** Sales Cloud, Opportunity Products, Line Items

Read it on the site: https://www.forcechallenges.com/interview-questions/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:**
```apex
// 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
                         WHERE Pricebook2Id = :pricebookId AND Product2Id = :productId
                           AND IsActive = true AND CurrencyIsoCode = :isoCode]) {
    pbeByProduct.put(p.Product2Id, p);
}
List<OpportunityLineItem> items = new List<OpportunityLineItem>();
for (Opportunity o : opps) {   // opps already have Pricebook2Id = :pricebookId
    PricebookEntry pbe = pbeByProduct.get(productId);
    if (pbe != null) {
        items.add(new OpportunityLineItem(OpportunityId = o.Id, PricebookEntryId = pbe.Id,
                  Quantity = 1, UnitPrice = pbe.UnitPrice));
    }
}
insert items;
```
(Remove the `CurrencyIsoCode` filter in single-currency orgs; the field does not exist there.)

**Hidden gotchas & must-remember points:**
- Switching price books deletes the line items.
- Do not set both UnitPrice and TotalPrice on insert; the API rejects it.
- Amount becomes read-only and driven by line items.
- Products must be active and in the opportunity's price book and currency.
- Standard Price Book must contain the product first.
- Reports on products need the Opportunities with Products report type.

**Best practice:** Set the price book first, load products in bulk, and let line items drive Amount.

---
Source: ForceChallenges · last updated 2026-11-02
