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
nandananandana 

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).
nandananandana
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}"/>
</apex:repeat>      
     </apex:pageBlock>
</apex:page>

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 )
     {
          System.debug('------------------>Cont---->'+cont.LastName);
     }
}

Let us know if you need more information


 
nandananandana
Thank you