I want to add products to a specific opportunity via APEX code. Not sure how I would be able to do that. I know how to create an opportunity and insert it into the environment but wanted to make it more specific so I can insert more child objects

1 Answer 1


Have a look at the Salesforce Product and Scehdule Objects Data Model.

You will want to insert a collection of OpportunityLineItem objects that are linked to the parent Opportunity via the OpportunityId field.

The OpportunityLineItem records should also use the same Pricebook2Id as the parent Opportunity and have a link to PricebookEntry records that correspond to the Porduct2 you want to link to.

You might find it easier to setup one or two sample Opportunity Product records via the UI and then examine them via SOQL to see what needs to be done.

  • 1
    and, if using multi-currency, the currency of the PBE / OLI must match the currency of the Opportunity
    – cropredy
    Aug 19, 2016 at 6:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .