Activity Forums Salesforce® Discussions How to store list of contacts in Salesforce associated to a particular account using Map? Reply To: How to store list of contacts in Salesforce associated to a particular account using Map?

  • Ajay Prakash

    Member
    January 10, 2018 at 4:30 am

    Hi subramanyam,

    Use below code, it will provide you a map of Account vs list of associated contacts.

    map<Account,list<Contact>> mapOfAccountVsContact = new map<Account,list<Contact>>();
    for(Account accObj : [select id, name, (select id,lastname from contacts) from Account]){
    mapOfAccountVsContact.put(accObj,accObj.Contacts);
    }
    system.debug('mapOfAccountVsContact::'+mapOfAccountVsContact);