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
Jeff GJeff G 

How to Access Opportunity's Products from Outside Opportunity?

Hi All,

I am creating a custom object that mirrors the information of an Opportunity object. I also want to copy over information related to the Opportunity's products (Opportunity Line Items) into custom fields, but I can't seem to access them.

I have a lookup relationship from the custom object to Opportunities, and there already exists a lookup relationship from Opportunity Products to their associated Opportunities. Unfortunately, I can't find a way to establish a lookup relationship for the other way around: from the Opportunity to the Opportunity Product. This prevents me from accessing each individual product I've added to a certain Opportunity.

Does anyone have any suggestions? Thanks!


Dushyant SonwarDushyant Sonwar
Hey Jeff,
As you said your custom object has lookup field  opportunity. You can create a map something like this.

list<Opportunity>Opplist =[select id,fieldname,(select id,fieldname from OpprtunityLineItems) from opportunity ];
Map<string,Opportunity> MapOfOpportunity = new Map<string,Opportunity>();
for(Opportunity OppObj : Opplist){
List<OpportunityLineItem> OppLineItems =MapOfOpportunity.get(customField(lookup Opportunity)).OpportunityLineItem; //Childs of that opportunity.

In this way you can access the Opportunity's Products.