You need to sign in to do that
Don't have an account?

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?
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.
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:
- PricebookEntryId - this tells the opportunity which pricebook / price you need for the particular product.
- Product2Id - The id of the "Product2" record.
- OpportunityId - The Id of the parent Opportunity record.
- Unit Price or Total Price (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunitylineitem.htm attempts to explain why)
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:
Hope that helps.
All Answers
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.
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:
- PricebookEntryId - this tells the opportunity which pricebook / price you need for the particular product.
- Product2Id - The id of the "Product2" record.
- OpportunityId - The Id of the parent Opportunity record.
- Unit Price or Total Price (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunitylineitem.htm attempts to explain why)
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:
Hope that helps.
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