How to display Parent, Child and Grandchild records in pageBlockTable in Salesforce?

How to display Parent, Child and Grandchild records in pageBlockTable in Salesforce?

Objects: Member, Interest, Equipment

Parent : Member
Member’s Child : Interest
Member’s Grandchild and Interest’s Child : Equipment

Visualforce page:

<apex:page sidebar="false" Controller="Sample" showHeader="true">
<apex:form >
<apex:pageBlock >
    <apex:pageBlockTable value="{!memberWrapperList}" var="W">
        <apex:column value="{!W.member.Name}"/>
        <apex:column value="{!W.member.Age__c}"/>
        <apex:column >
            <apex:pageblockTable value="{!W.intrstList}" var="I">
                <apex:column value="{!I.Name}"/>
                <apex:column >
                    <apex:pageBlockTable value="{!I.Equipments__r}" var="E">
                        <apex:column value="{!E.Name}"/>
                        <apex:column value="{!E.Serial_Number__c}"/>
                    </apex:pageBlockTable>
                </apex:column>
            </apex:pageblockTable>
        </apex:column>
    </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Apex Controller:

public class Sample { 
    public List<Member__c> memberList { get; set; }
    public List<Interest__c> interestList { get; set; }
    public Map<Id, List<Interest__c>> memIdInterestListMap { get; set; }
    Set<Id> memberIds = new Set<Id>();
    Map<Id, Member__c> memberMap = new Map<Id, Member__c>();
    
    public List<MemberWrapper> memberWrapperList { get; set; }

    public Sample() {   
        memberList = [SELECT Age__c, Name FROM Member__c];
        memIdInterestListMap = new Map<Id, List<Interest__c>>();
        memberWrapperList = new List<MemberWrapper>();
        if(memberList.size() > 0) {
            for(Member__c mem : memberList) {
                memberIds.add(mem.Id);
                memberMap.put(mem.Id, mem);
            }
            interestList = [SELECT Name, Member__c, (SELECT Name, Serial_Number__c FROM Equipments__r) FROM Interest__c WHERE Member__c IN : memberIds];
            system.debug('Interest List is ' + interestList);
        }
        if(interestList.size() > 0) {
            for(Interest__c intrst : interestList) {
                if(!memIdInterestListMap.containsKey(intrst.Member__c)){
                    memIdInterestListMap.put(intrst.Member__c, new List<Interest__c>());
                }
                memIdInterestListMap.get(intrst.Member__c).add(intrst);
            }
            for(Id interestId : memIdInterestListMap.keySet()) {
                memberWrapperList.add(new MemberWrapper(memberMap.get(interestId), memIdInterestListMap.get(interestId)));
            }
        }
    } 
    
    public class MemberWrapper {
        public Member__c member { get; set; }
        public List<Interest__c> intrstList { get; set; }
        
        public MemberWrapper(Member__c member, List<Interest__c> intrstList) {
            this.member = member;
            this.intrstList = intrstList;
        }
    }
}

Output:

Leave a Reply