Displaying child record from parent in Salesforce

Displaying child record from parent in Salesforce

Sample Code:

Visualforce page:



<apex:page controller=”sample”>
    <apex:pageBlock >
        <apex:pageBlockTable value=”{!acct}” var=”a”>
            <apex:column value=”{!a.Name}”/>
            <apex:repeat value=”{!a.Contacts}” var=”c”>
                <apex:column value=”{!c.Name}”/>
            </apex:repeat>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>



Apex Controller:


public class sample
{   
    public List<Account> acct {get;set;}
    public sample()
    {
        String soql = ‘SELECT Name, (SELECT Name, Email FROM Contacts) FROM Account LIMIT 5’;
        acct = Database.Query(soql);
    }   
}

Output:

Leave a Reply