How to fetch child records from parent in SOQL in controller in Salesforce?

Sample Code:

Visualforce page:

<apex:page controller="Sample" sidebar="false">
<apex:form >
    <apex:pageBlock >
        <apex:pageblockTable value="{!memb}" var="m">
            <apex:column value="{!m.Name}"/>
        </apex:pageblockTable>
        <apex:pageblockTable value="{!interst}" var="i">
            <apex:column value="{!i.Name}"/>
        </apex:pageblockTable>       
    </apex:pageBlock>
</apex:form>   
</apex:page>


Apex Controller:

public class Sample
{
    public List<Member__c>  memb { get; set; }
   
    public List<Interest__c>  interst { get; set; }
   
    public sample()
    {
       List<Interest__c> temp = new List<Interest__c>();
       memb = [SELECT Name, (SELECT Name from Interest__r) from Member__c];
       for(Member__c m : memb)
       {
           System.debug('Interests are ' + m.Interest__r);
           for(Interest__c i : m.Interest__r)
           {
               System.debug('Interest is ' + i);
               temp.add(i);
           }              
       }
       interst = temp;           
    } 
}


Output:


No comments:

Post a Comment