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
Anoop Kumar 31Anoop Kumar 31 

write a soql query to fetch 500 account records with all their related contacts and prepare a collection of accountid and its related contacts

Please answer anyone ??
Best Answer chosen by Anoop Kumar 31
v varaprasadv varaprasad
Hi Anoop,

Try This : 
 
List<Account> lstAccs = [SELECT ID, Name FROM ,(Select Id ,LastName from Contacts) Account LIMIT 500];

//Map of accountid and its related contacts
Map<Id, list<contact>> accMap= new Map<Id, list<contact>>();

for(account acc : lstAccs){
    if(acc.Contacts.size()>0)
    accMap.put(acc.id,acc.Contacts);
}

system.debug('==accMap=='+accMap);

More Info : 

https://www.youtube.com/watch?v=Us9x9GyuRj8&t=5s

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 

All Answers

Raj VakatiRaj Vakati
Map<Id, Account> accMap= new Map<Id, Account>([SELECT ID, Name FROM Account LIMIT 500]);

Or 
Map<Id, Account> accMap= new Map<Id,Account>() ; 

for(Account aa:[SELECT ID, Name FROM Account LIMIT 500]){
accMap.put(aa.Id ,aa);
}

 
Anoop Kumar 31Anoop Kumar 31
Raj Vakati

But where is related contacts in this Query
Raj VakatiRaj Vakati
Map<Id, Account> accMap= new Map<Id, Account>([SELECT ID, Name FROM ,(Select Id ,LastName from Contacts) Account LIMIT 500]);

 
v varaprasadv varaprasad
Hi Anoop,

Try This : 
 
List<Account> lstAccs = [SELECT ID, Name FROM ,(Select Id ,LastName from Contacts) Account LIMIT 500];

//Map of accountid and its related contacts
Map<Id, list<contact>> accMap= new Map<Id, list<contact>>();

for(account acc : lstAccs){
    if(acc.Contacts.size()>0)
    accMap.put(acc.id,acc.Contacts);
}

system.debug('==accMap=='+accMap);

More Info : 

https://www.youtube.com/watch?v=Us9x9GyuRj8&t=5s

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Anoop,

Have a look at the code below:

public class relatedcontact {
public static void relatedlist()
{
    List<Account> acList=[SELECT Name,(SELECT lastname from Contacts )from Account limit 500];
    //System.debug('account list'+acList);
    List<Contact> conList=new List<Contact> ();  
    Map<Id,List<Contact>> mymap=new Map<Id,List<Contact>>();
        for(Account ac:acList)
    {
        if(ac.contacts.size()>0)
        {
          conList.addall(ac.Contacts);
          //this list will contain all the contacts related to some particular accounts.
          //we are not aware of which contact is related to which account,it only gives list of related contacts
          mymap.put(ac.id,ac.Contacts);  
          //Whereas using  Map collection here we have account's id and list of Contacts related to that account.

        }
    }
     System.debug('Related list of Contacts'+conList);
    System.debug('Map Collection of Account id and list of Contact'+mymap);

}

Hope this explanation will resolve your query.Mark it as best answer if you find it helpful.

Thanks.
Ajay Dubedi