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
Srinivas223Srinivas223 

soql queries

Hello All,

I have 3 custom objects namely Parent__c, Child__c, GrandChild__c, I can get the id of the parent from visualforce page. I want to display the grand child records in the visualforce page. I appreciate if anyone could help me with this.

Thank you
Best Answer chosen by Srinivas223
Gururaj BGururaj B
Hi Srinivas,
Below is the Visual page and class for your requirement using controller extension. In my example i have used the following objects. You just replace with your objects and fields.You shuld have your parent id in url. Please mark as best answers if it works for you..
Account  - Parent
Opportunity - Child
OpportunityLineItems - Grand child

VP:
<apex:page standardController="Account" extensions="GrandchildDisplay">
    <Apex:pageblock >
    <apex:pageBlockTable value="{!oplist}" var="ol">
        <apex:column value="{!ol.Id}"/>
        <apex:column value="{!ol.name}"/>
        <apex:column value="{!ol.Quantity}"/>
        <apex:column value="{!ol.product2id}"/>
    </apex:pageBlockTable>
    </Apex:pageblock>
</apex:page>


Class:
public class GrandchildDisplay {
    public list<OpportunityLineItem> oplist {set;get;}
    public GrandchildDisplay(apexpages.StandardController std){
         list<opportunity> opp = new list<opportunity>();    
    list<OpportunityLineItem> tempoplist = new list<OpportunityLineItem>();
id accid = apexpages.currentPage().getparameters().get('Id');
    opp =[select id,(select id,name,OpportunityId,Product2Id,Quantity from OpportunityLineItems) oplst from Opportunity where accountid=:Accid];
    system.debug('accountid ' + accid + ' opporty ' +opp);
for(opportunity op:opp)
{
    tempoplist.add(op.OpportunityLineItems);    
}    
    oplist=tempoplist;
    }
}
 

All Answers

Gururaj BGururaj B
Hi Srinivas,
Below is the Visual page and class for your requirement using controller extension. In my example i have used the following objects. You just replace with your objects and fields.You shuld have your parent id in url. Please mark as best answers if it works for you..
Account  - Parent
Opportunity - Child
OpportunityLineItems - Grand child

VP:
<apex:page standardController="Account" extensions="GrandchildDisplay">
    <Apex:pageblock >
    <apex:pageBlockTable value="{!oplist}" var="ol">
        <apex:column value="{!ol.Id}"/>
        <apex:column value="{!ol.name}"/>
        <apex:column value="{!ol.Quantity}"/>
        <apex:column value="{!ol.product2id}"/>
    </apex:pageBlockTable>
    </Apex:pageblock>
</apex:page>


Class:
public class GrandchildDisplay {
    public list<OpportunityLineItem> oplist {set;get;}
    public GrandchildDisplay(apexpages.StandardController std){
         list<opportunity> opp = new list<opportunity>();    
    list<OpportunityLineItem> tempoplist = new list<OpportunityLineItem>();
id accid = apexpages.currentPage().getparameters().get('Id');
    opp =[select id,(select id,name,OpportunityId,Product2Id,Quantity from OpportunityLineItems) oplst from Opportunity where accountid=:Accid];
    system.debug('accountid ' + accid + ' opporty ' +opp);
for(opportunity op:opp)
{
    tempoplist.add(op.OpportunityLineItems);    
}    
    oplist=tempoplist;
    }
}
 
This was selected as the best answer
Gururaj BGururaj B
small correction, just use addall instead of add inside the for loop
tempoplist.addall(op.OpportunityLineItems);