+ Start a Discussion

Hi... How to get accounts records along with related contact records?

Lalit Mistry 21Lalit Mistry 21
Hi Nandana,

If you are looking for a SOQL then beloq query should work for you.
[SELECT Id, Name, (SELECT Id FROM Contacts) FROM Account]

You may modify the above query to include your columns of interest along with filter (where clause).
Thank you.
Amit Chaudhary 8Amit Chaudhary 8
If you want to show on VF page then try below code
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false">
    <apex:pageBlock >
          <apex:repeat value="{!accounts}" var="a">
<apex:pageBlockSection title="{!a.name}"></apex:pageBlockSection>
  <apex:relatedList list="Contacts" subject="{!a.Id}"/>

For SOQL you can try below query
SELECT Id, Name, (SELECT Id FROM Contacts) FROM Account

For Apex code like below
List<Account> lstAccount = [ SELECT Id, Name, (SELECT Id,LastName FROM Contacts) FROM Account ];
For( Account acc : lstAccount )
     List<Contact> lstCont = acc.Contacts;
     For(Contact cont : lstCont )

Let us know if you need more information

Thank you