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
Sfdc CoupleSfdc Couple 

How to do mapping account id and email and all other associated records from contact ?

Deepali KulshresthaDeepali Kulshrestha
Hi,

I've gone through your requirement and you can find the apex code for mapping account and contact below:



List<Account> allaccounts=new List<Account>([select id,Name from Account limit 1000]);

List<Contact> allcontacts=new List<Contact>([select id,Name,Email,AccountId from Contact where AccountID IN:allaccounts]);

     for(Contact contacts:contactsList)
        {
             if(ConAccMap.containsKey(contacts.AccountId))
               {   
                ConAccMap.get(contacts.AccountId).add(contacts);
                   
               }
            else 
               {   
                
                ConAccMap.put(contacts.AccountId,new list<contact>{contacts}); 
                ConAccMap.get(contacts.AccountId).add(contacts);
               }
        }


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Sfdc CoupleSfdc Couple
Hi Deepali,

                Thanks. I 've done this yesterday. Then i checked with your coding. I got an error ContactList doesn't exist!


My code: 

           Map<Id,List<String>> accountemailmap = new Map<Id,List<string>>();
for(contact con : [Select id,accountId,email from contact where email!=null and accountId!=null]){
    List<String> emaillst = accountemailmap.get(con.AccountId);
    if(emaillst == null)
        emaillst = new List<String>();
    emaillst.add(con.email);
    accountemailmap.put(con.accountId,emaillst);
    system.debug('accountemailmap'+accountemailmap);
}


Regards,

Sfdccouple
sfdccouple.com