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
New_DeveloperNew_Developer 

Calling child object records in a visulaforce page

How to reference a child object and call the child object records in a visualforce page with master object as standard controller

SantoshiSantoshi

In case of child object and master object as the standard controller, you can use the pageblock table or data table for child records. In the extension class retrieve the child record by SOQL query and store the retrieved records in a list variable.  Use the getter of this list variable in the page block/data table to display the records on vf page.

 

For eg:

VF page

 

<apex:page standardController="MasterObject" extensions="Class1">

 

<apex:pageblocktable value="{!childrecords}"  var="rec">

<apex:column value="{!rec.name}"/>

 

</apex:page>

 

class

 

public class class1

{

 

public list<ChildRecordObject> childrecords {get;set;} /**  Retrieve the child records and store it in this varaiable which is referenced on page.I am not sure of your scenario i.e. when do you want to display the records on page. You can also write SOQL and assign values to this variable in the constructor and the child records will be displayed on the page load itself.. */

 

}

Shashikant SharmaShashikant Sharma

You can directly Accesss Child records using the RelationshipName, see this example.

 

<apex:page standardController="Account">
<apex:pageBlock>
You're looking at some related lists for {!account.name}:
</apex:pageBlock>
<apex:relatedList list="Opportunities" />
<apex:relatedList list="Contacts">
<apex:facet name="header">Titles can be overriden with facets</apex:facet>
</apex:relatedList>
<apex:relatedList list="Cases" title="Or you can keep the image, but change the text"
/>
</apex:page>

 In this Opportunities,Contacts,Cases are relationship Names. These Relationship Names will give you all the records associated with the account that you wil see for contact , oopurtunity and case. If you want some specific records then you need to use a extention class and Use SOQL and prepare a list and show it on the page.