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
RV328RV328 

Account and related contact in pageblocktable - PLEASE HELP

I am trying to print Account Name and related cotact name in the other columns of pageblocktable

<apex:page standardController="Account" extensions="CustAccContExtn" >
    <apex:form >
        <apex:pageBlock title="A/c and Cont in Table" >
            <apex:pageblockButtons location="bottom">
                <apex:commandButton value="Fetch Account" action="{!getAccountsResc}"/>
            </apex:pageblockButtons>
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!allAccs}" var="ac">
                    <apex:column value="{!ac.Name}" headerValue="Account Name"/>
                    
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

public class CustAccContExtn {
    
    public List<Account> allAccs {get;set;}
    
    public PageReference getAccountsResc() {
        allAccs = [SELECT id, Name FROM Account];
        return null;
    }
    public CustAccContExtn(ApexPages.StandardController controller) {
        allAccs =  new List<Account>();
    }
}


What update do I need to make on select statement to pull the name of all contact associated for an account and how do i call them in apex:column

Please Help!

 
Best Answer chosen by RV328
PuneetsfdcPuneetsfdc
try below code


public class CustAccContExtn {
    
    public List<Account> allAccs {get;set;}

    public PageReference getAccountsResc() {
        allAccs = [SELECT id, Name,(Select Id, Name From Account.Contacts) FROM Account];
        return null;
    }
    public CustAccContExtn(ApexPages.StandardController controller) {
        allAccs =  new List<Account>();
    }
}



<apex:page standardController="Account" extensions="CustAccContExtn" >
    <apex:form >
        <apex:pageBlock title="A/c and Cont in Table" >
            <apex:pageblockButtons location="bottom">
                <apex:commandButton value="Fetch Account" action="{!getAccountsResc}"/>
            </apex:pageblockButtons>
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!allAccs}" var="ac">
                    <apex:column value="{!ac.Name}" headerValue="Account Name"/>
                    <apex:column headerValue="Contacts">
                        <apex:repeat value="{!ac.Contacts}" var="cont">
                            <apex:outputText value="{!cont.Name}"/><br/>
                        </apex:repeat>                    
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>