+ Start a Discussion
Harsh P.Harsh P. 

Dislplay account ant it's related contact on VF page

Please help for following VF page:
I want Output like this :
-----------------------------------------------
AccountName1         Contact 1
                                   Contact 2

AccountName2         Contact 1
                                   Contact 2
                                   Contact 3

-------------------------------------------------
Best Answer chosen by Harsh P.
SandhyaSandhya (Salesforce Developers) 
Hi,

Below is the sample code try to modify according to your requirement.
 
<apex:page controller="ContactsVisualforceController" standardStylesheets="false">
    <apex:pageBlock>
        <apex:repeat value="{!displayAccounts}" var="acc">
            <dl>
                <dt>Account Name:</dt>
                <dd><apex:outputText  value="{!acc.Name}"/></dd> 
            </dl>
            
            <dl><dt>Contacts:</dt></dl>
            
            <apex:repeat value="{!acc.Contacts}" var="cont">
                <dl>
                    <dd><apex:outputText value="{!cont.Name}"/></dd>
                </dl>
            </apex:repeat>    
        </apex:repeat>
        
    </apex:pageBlock>
</apex:page>
 
public with sharing class ContactsVisualforceController {
    public list<Account> displayAccounts {get; set;}
    public ContactsVisualforceController(){
        displayAccounts = [select id,name,(select id,name from Contacts) from Account];
    }
}

Also refer below link.

https://developer.salesforce.com/forums/?id=906F000000096qGIAQ
 
​​​​​​​Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya
 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

Below is the sample code try to modify according to your requirement.
 
<apex:page controller="ContactsVisualforceController" standardStylesheets="false">
    <apex:pageBlock>
        <apex:repeat value="{!displayAccounts}" var="acc">
            <dl>
                <dt>Account Name:</dt>
                <dd><apex:outputText  value="{!acc.Name}"/></dd> 
            </dl>
            
            <dl><dt>Contacts:</dt></dl>
            
            <apex:repeat value="{!acc.Contacts}" var="cont">
                <dl>
                    <dd><apex:outputText value="{!cont.Name}"/></dd>
                </dl>
            </apex:repeat>    
        </apex:repeat>
        
    </apex:pageBlock>
</apex:page>
 
public with sharing class ContactsVisualforceController {
    public list<Account> displayAccounts {get; set;}
    public ContactsVisualforceController(){
        displayAccounts = [select id,name,(select id,name from Contacts) from Account];
    }
}

Also refer below link.

https://developer.salesforce.com/forums/?id=906F000000096qGIAQ
 
​​​​​​​Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya
 
This was selected as the best answer
Harsh P.Harsh P.
Thanks Sandhya !