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
Brian Oconnell WPDBrian Oconnell WPD 

What is the child relationship name on Product for Opp Line Items?

I am trying to make a Visualforce custom related list of Opportunities related to a Product but I can't find the child relationship name.

Example using Account and Case:
<apex:page standardController="Account">
        <apex:repeat var="cases" value="{!Account.Cases}">
But I want to replace Account with Product2, and Cases with Opportunities.  How?

Thank you
 
Best Answer chosen by Brian Oconnell WPD
James LoghryJames Loghry
Gotcha.  Unfortunately that falls in the "complicated relationships that standard controllers don't quite support" bucket.  The relationship from Product to Opportunity looks like Product->PricebookEntry->Opportunity Product / Opportunity Line Item->Opportunity.  In this case, you're going to need a Visualforce extension controller that populates a list of opportunities for you to display in the VF page. Your controller might look like the following:
 
public with sharing MyExtension{

    private Product2 prod {get; set;}

    //Read only list of opportunities for VF display
    public List<Opportunity> opportunities {get; private set;}

    //Extension constructor
    public MyExtension(ApexPages.StandardController sc){
        prod = (Product2)sc.getRecord();

        opportunities = new List<Opportunity>();
        for(OpportunityLineItem oli : 
            [Select 
                Opportunity.Name
                ,Opportunity.StageName 
             From
                OpportunityLineItem 
             Where
                PriceBookEntry.Product2Id = :prod.Id]){
            opportunities.add(oli.Opportunity);
        }
    }
}

Then in your Visualforce page, you would use {!opportunities} for displaying the query results.

All Answers

James LoghryJames Loghry
"OpportunityLineItems" is what you're looking for.
 
<apex:page standardController="Opportunity">
        <apex:repeat var="cases" value="{!Opportunity.OpportunityLineItems}">

 
James LoghryJames Loghry
Replace cases with oli or another variable, of course :)
Brian Oconnell WPDBrian Oconnell WPD
That would work on an Opportuntiy page, but I need the list on a Product page.
James LoghryJames Loghry
Gotcha.  Unfortunately that falls in the "complicated relationships that standard controllers don't quite support" bucket.  The relationship from Product to Opportunity looks like Product->PricebookEntry->Opportunity Product / Opportunity Line Item->Opportunity.  In this case, you're going to need a Visualforce extension controller that populates a list of opportunities for you to display in the VF page. Your controller might look like the following:
 
public with sharing MyExtension{

    private Product2 prod {get; set;}

    //Read only list of opportunities for VF display
    public List<Opportunity> opportunities {get; private set;}

    //Extension constructor
    public MyExtension(ApexPages.StandardController sc){
        prod = (Product2)sc.getRecord();

        opportunities = new List<Opportunity>();
        for(OpportunityLineItem oli : 
            [Select 
                Opportunity.Name
                ,Opportunity.StageName 
             From
                OpportunityLineItem 
             Where
                PriceBookEntry.Product2Id = :prod.Id]){
            opportunities.add(oli.Opportunity);
        }
    }
}

Then in your Visualforce page, you would use {!opportunities} for displaying the query results.
This was selected as the best answer
James LoghryJames Loghry
Also, your VF markup would look like the following:
 
<apex:page standardController="Product2" extensions="MyExtension">
        <apex:repeat var="opp" value="{!opportunities}"></apex:repeat>
</apex:page>

 
Brian Oconnell WPDBrian Oconnell WPD
Thank you!  That works.