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
Sparky JaneSparky Jane 

How to avoid writing query in for loop?

Can someone please help me out from avoiding writing query in for loop (for(Account ac:accList)) ?


public class AccountsAndContacts{
    public void acc(){
        Map<Id,List<Contact>> contactslist = new Map<Id,List<Contact>>();
        List<Account> accList = [SELECT name from Account];
        for(Account ac:accList){
            list<Contact> con=[select name from Contact where Account.Id=:ac.Id];
            contactslist.put(ac.Id,con);          
        }
        for(Account ac:accList){
            System.debug('Account Name:  '+ac.name);
            System.debug('Contacts List:  '+contactslist.get(ac.Id));         
        }
    }
}
Best Answer chosen by Sparky Jane
bob_buzzardbob_buzzard
Yeah, that should work okay.  If you want to test it you can tweak to :

public class AandCWithoutQuery{
    public void acc(){
         Map<Id,List<Contact>> contactslist = new Map<Id,List<Contact>>();
        List<Account> accList = [SELECT name, (select id, name from Contacts) from Account];

        for(Account ac:accList){
             contactslist.put(ac.Id, ac.contacts);
        }

       for(Account ac:accList){
             List<Contacts> accConts=contactslist.get(ac.Id);
              System.debug('Account Name:  '+ac.name);
            System.debug('Contacts List:  '+accConts);
        }

    }
}


All Answers

Ankit AroraAnkit Arora
Will not be able to write the exact code (on mobile), but it's pretty simple. You can write innerquery instead of first querying account and then fetching contacts from each account. Inner query will return you account and list of contacts associated with them. Please google how to write inner query, it's really very simple.
bob_buzzardbob_buzzard
You can pull the contacts associated with the account through the first SOQL query:

List<Account> accList = [SELECT name, (select id, name from Contacts) from Account];

This will mean that your accounts each have a child list of contacts, which you can then iterate without worrying putting the items into a map:
for(Account ac:accList){
        System.debug('Account Name:  '+ac.name);
        System.debug('Contacts List:  '+ac.contacts);         
}



Grazitti TeamGrazitti Team
Hi RVL,

Below is modification of your code. 

List<Account> accList = [SELECT name from Account];
        List<ID> accIDsList = new List<ID>();

        for(Account  ac : accList ) {
        accIDsList.add(ac.Id);
        }
list<Contact> con=[select name from Contact where Account.Id=:accIDsList];

now the Query is outside of the for loop and will give the same results. 

let us know if you have any question .
please don't Forget to Mark this as your best answer if it works fine for you

Regards,
Grazitti Team
Web: www.grazitti.com


Sparky JaneSparky Jane
Hi bob_buzzard,
        The answer that you've given, helped me a lot. But I wanna do that code without missing "Map<id,List>". Is the following Code  correct or not!!

public class AandCWithoutQuery{
    public void acc(){
         Map<Id,List<Contact>> contactslist = new Map<Id,List<Contact>>();
        List<Account> accList = [SELECT name, (select id, name from Contacts) from Account];
        for(Account ac:accList){
             contactslist.put(ac.Id, ac.contacts);
              System.debug('Account Name:  '+ac.name);
            System.debug('Contacts List:  '+ac.contacts);
        }

    }
}

bob_buzzardbob_buzzard
Yeah, that should work okay.  If you want to test it you can tweak to :

public class AandCWithoutQuery{
    public void acc(){
         Map<Id,List<Contact>> contactslist = new Map<Id,List<Contact>>();
        List<Account> accList = [SELECT name, (select id, name from Contacts) from Account];

        for(Account ac:accList){
             contactslist.put(ac.Id, ac.contacts);
        }

       for(Account ac:accList){
             List<Contacts> accConts=contactslist.get(ac.Id);
              System.debug('Account Name:  '+ac.name);
            System.debug('Contacts List:  '+accConts);
        }

    }
}


This was selected as the best answer