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:



5 comments:

  1. Hi, its very good article, and if i want to display Member(object) data in order,in above PageBlocktable how?

    Thanks,

    Vivek.

    ReplyDelete
    Replies
    1. You have to use comparable interface for sorting.

      Cheers!!!

      Delete
  2. Hi Magulan! I tried to work on your code. But there's more than 50,000 records so I'm getting error for the same. How shall I work that out? I need to get all child and grandchild for a particular Parent id.

    ReplyDelete
    Replies
    1. You cannot support more than 50000 records. That is Salesforce limitation.

      Cheers!!!

      Delete