function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JJamesJJames 

Add objects to a related list in apex

I am wondering how to assign an object to be placed into a related list of an object.  I am trying to add products to the product list in an opportunity from a visualforce page but im not sure how to add this object in the controller to the opportunity related list 'Products'.  How would I add a product I have to the related list of a specific opportunity?
Best Answer chosen by JJames
James LoghryJames Loghry
So, Opportunity Products are probably the trickiest of all the relationships in Salesforce. In other words, you're starting with a tough cookie. I'll get to that in a second.

First, if you want to add a related object to a parent object's related list, you simply need to set the appropriate lookup or master detail relationship.  
 
Contact c = new Contact(AccountId='123');  
insert;



This will add a contact to account 123's related list (assuming 123 was an actual account id, which it isn't.

That being said, for Opportunity Products (or Opportunity Line Items as they're also called), you'll need a few additional fields: All of these fields factor into the parent Opportunity and the total amount / revenue / pipeline, by the way.

Once you specify all the fields you'll end up with apex similar to the following:
 
​List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId=oId,Product2Id=pId,PriceBookEntryId=pbeId,UnitPrice=0.00);
oliList.add(oli);
insert oliList;

Hope that helps.

All Answers

James LoghryJames Loghry
So, Opportunity Products are probably the trickiest of all the relationships in Salesforce. In other words, you're starting with a tough cookie. I'll get to that in a second.

First, if you want to add a related object to a parent object's related list, you simply need to set the appropriate lookup or master detail relationship.  
 
Contact c = new Contact(AccountId='123');  
insert;



This will add a contact to account 123's related list (assuming 123 was an actual account id, which it isn't.

That being said, for Opportunity Products (or Opportunity Line Items as they're also called), you'll need a few additional fields: All of these fields factor into the parent Opportunity and the total amount / revenue / pipeline, by the way.

Once you specify all the fields you'll end up with apex similar to the following:
 
​List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId=oId,Product2Id=pId,PriceBookEntryId=pbeId,UnitPrice=0.00);
oliList.add(oli);
insert oliList;

Hope that helps.
This was selected as the best answer
JJamesJJames
James, you're a lifesavor.  Thank you!
Liz MalmanLiz Malman
Hi James,

I'm wondering how I can add an opportunity to my related opportunities list on an event using apex. Any help would be great.

Thanks,
Liz