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
Yamile Pacheco CuevaYamile Pacheco Cueva 

Grandchild field to appear in parent visual force page

I am building a visualforce page that has the Parent__c record and the Child_Record__c in a form. The goal is to create a page where the users can edit the Parent, the Child and the Grand Child records without having to open related lists.

So far I am able to show the parent and the child in the form but I am having no success in showing the grandchild records. 
I have 3 page blocks
Parent__c
  Child__c
    Grandchild__c (with no fields displayed because I do not know how to access the grandchild)

I have very limited knowledge of apex and played around with some sample apex classes but have had no success retrieving the fields from the grandchild records

How can I access the grandchild fields in an visualforce page? Do I really need an apex class or can it be done just using the standard controller? If I need a class, can someone please provide a sample class for this scenario?

Thanks!
Maharajan CMaharajan C
Hi  Yamile Pacheco Cueva, 

Please refer the below sample class i hope you can understand.

Use the Child object in Apex class method to get the Parent details and grand child details.

In the below example order of object is Assessment - > ParentQuestion -> child Answer -> grand child.

Public class AccountDisplatRecClsExtn{
Public id Current_Acc_Id;
public AccountDisplatRecClsExtn(ApexPages.StandardController controller) {
Current_Acc_Id = controller.getRecord().id;
    }
     
  public List<Question__c> getcontList(){
   List <Question__c> clist= [select id,Name,AssessmentId__c,AssessmentId__r.Name,External_ID__c,Friendly_N
ame__c,Question_Plain__c,Question_Style__c,Sort_Order__c,(Select Id,Name,Red_Flag__c,Scoring__c from answ
ers__r
) from Question__c where AssessmentId__c=:Current_Acc_Id ORDER by Sort_Order__c ASC];
   return clist;    
  //answers__r --> relationship name
  }
    }

Visualsforce Page:


<apex:page standardcontroller="Assessment__c" extensions="AccountDisplatRecClsExtn" renderAs="pdf">
<h1>Welcome to Raj Test!</h1>
<p>Your Assessment details are:</p>
   <table>
   <tr><th>Assessment Name</th>
   <td> - </td>
   <td><apex:outputText value="{!Assessment__c.Name}" /></td></tr>
    <tr><th>Start Text</th>
     <td> - </td>
   <td><apex:outputText value="{!Assessment__c.Start_Text__c}"/></td></tr>
    <tr><th>End Text</th>
     <td> - </td>
   <td><apex:outputText value="{!Assessment__c.End_Text__c}"/></td>
  </tr>
  </table>
   
<apex:pageblock title="Question and Answer Details">
 
<apex:pageblockTable value="{!contList}" var="con" border="1px solid black">
 
  
   <apex:column style="width:50px" value="{!con.Name}"/>
   <apex:column style="width:200px" value="{!con.AssessmentId__r.Name}"/>
   <apex:column style="width:250px" value="{!con.Friendly_Name__c}"/>
   <apex:column style="width:250px" value="{!con.Question_Plain__c}"/>
   <apex:column style="width:250px" value="{!con.Question_Style__c}"/>
   <apex:column headerValue="Answer Details">
  
   <apex:pageblockTable value="{!con.answers__r}" var="ans" border="0.5px solid black">
   <apex:column style="width:400px" value="{!ans.Name}"/>
   <apex:column style="width:400px" value="{!ans.Red_Flag__c}"/>
   <apex:column style="width:400px" value="{!ans.Scoring__c}"/>
   
   </apex:pageblockTable>
   </apex:column>
 </apex:pageblockTable>
 
</apex:pageblock>
 
</apex:page>

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
​Raj
Yamile Pacheco CuevaYamile Pacheco Cueva
Thank you. Does that mean that the button has to be in the child (questions) page layout? or can I place the button in the parent (assesment) layout?
Maharajan CMaharajan C
  • If you are using the <apex:page standardcontroller="Assessment__c" extensions="AccountDisplatRecClsExtn" renderAs="pdf"> then the button will be available in assesment.
  • If you are using the <apex:page standardcontroller="Question__c" extensions="AccountDisplatRecClsExtn" renderAs="pdf"> then the button will be available in Question.
Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
​Raj
Yamile Pacheco CuevaYamile Pacheco Cueva
I get this error: Unknown property 'VisualforceArrayList.answers__r'

For this: 
        <apex:pageBlock>
            <apex:pageBlockSection title="Sample">
                <apex:pageBlockTable value="{!clist.answers__r}" var="a">
                    <apex:column value="{!clist.Name}"/>
                    <apex:column headerValue="Name">
                        <apex:inputField value="{!a.Name}"/>
                    </apex:column>                    
                </apex:pageBlockTable>               
            </apex:pageBlockSection>
        </apex:pageBlock> 


  public List<Questions__c> getquestionsList(){
   List <Questions__c> questionlist= [select id,Name,Assesment__r.Name,
        (Select Id,Contact__c,Status__c,Comments__c from Answers__r) 
        from Questions__c 
        where Assesment_Id__c=:Current_Assesment_Id ORDER by Name ASC];
   return questionslist;   


 
Maharajan CMaharajan C
<apex:pageBlock>
       
    <apex:pageBlockSection title="Sample">
                <apex:pageBlockTable value="{!questionlist.answers__r}" var="a">
                    <apex:column value="{!questionlist.Name}"/>
                    <apex:column headerValue="Name">
                        <apex:inputField value="{!a.Name}"/>
                    </apex:column>                    
                </apex:pageBlockTable>               
            </apex:pageBlockSection>
        </apex:pageBlock>