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
Aneesh AllumallaAneesh Allumalla 

How to get Accounts with its related Contacts under it in a vf page.

Hi!
I have a requirement where I need to get account name and under it ,should have the related Contacts with thier names in rows. It should be in a visual force page.
Ex:
Aneesh
  A1
  A2

Charan 
  C1
  C2
.
.
.
.
.
Like this
Best Answer chosen by Aneesh Allumalla
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Aneesh,
Try this one and modify it according to your requirements.
 
<apex:page standardController="Account" recordSetVar="Acct">
    <apex:pageBlock >
        <table>
            <apex:repeat value="{!Acct}" var="acc">
                <tr>
                    <td><apex:outputText ><b>{!acc.name}</b></apex:outputText></td>
                </tr>
                <apex:repeat value="{!acc.Contacts}" var="cont">
                    <tr>
                        <td><apex:outputText value="{!cont.Name}"/></td>
                    </tr>
                </apex:repeat>
            </apex:repeat>
        </table>
    </apex:pageBlock>
</apex:page>

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Aneesh,
Try this one and modify it according to your requirements.
 
<apex:page standardController="Account" recordSetVar="Acct">
    <apex:pageBlock >
        <table>
            <apex:repeat value="{!Acct}" var="acc">
                <tr>
                    <td><apex:outputText ><b>{!acc.name}</b></apex:outputText></td>
                </tr>
                <apex:repeat value="{!acc.Contacts}" var="cont">
                    <tr>
                        <td><apex:outputText value="{!cont.Name}"/></td>
                    </tr>
                </apex:repeat>
            </apex:repeat>
        </table>
    </apex:pageBlock>
</apex:page>

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
Aneesh AllumallaAneesh Allumalla
Hi Devi,
I am not able to get the correct output.
Here is what I am seeing. All these are Account Names
User-added image
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
I tried this one in my org.Am able to see both accounts and contacts in vf page.Check permissions and also if the org has contacts or not.
Aneesh AllumallaAneesh Allumalla
All the accounts have contatcs. In fact it returned only the contacts that have contacts.
Could you please let me know what did you mean by permissions?
Aneesh AllumallaAneesh Allumalla
*Account
 
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
check if the logged in user have permission to view contacts
Aneesh AllumallaAneesh Allumalla
The role is System administrator. So I think the problem is not with the permissions.
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
I used the same code.it is working for me
User-added image
Aneesh AllumallaAneesh Allumalla
Hi Devi.
Thanks for your help.
It worked.
 
Deepali KulshresthaDeepali Kulshrestha
Hi Aneesh,

I have gone through your question. You can easily get an account with its related contact.
In this, you can get one more functionality i.e. on click on account name or contact name
you can visit the account or contact page.

Code ==>
<apex:page standardController="Account" recordSetVar="accounts">
<apex:pageblock >
    <apex:repeat var="a" value="{!Accounts}" rendered="true" id="account_list">
        <li>
            <apex:outputLink >
                <b>{! a.Name}</b>
            </apex:outputLink>
            <br/>
            
           <apex:repeat  var="c" value="{!a.Contacts}" rendered="true" id="contacts_list">
               <apex:outputLink value="/{!c.ID}" >
                {! c.Name}
            </apex:outputLink>
            </apex:repeat> 
        </li>
    </apex:repeat>
</apex:pageblock>
</apex:page>


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com