+ Start a Discussion
Sunil Kumar 545Sunil Kumar 545 

I want to create a Map (collection), which has Account id as a Key and list of contacts as value.

Best Answer chosen by Sunil Kumar 545
Ajay K DubediAjay K Dubedi
Hi Sunil 

Try this one
List<Contact> contactList = new List<Contact>();
contactList = [SELECT AccountId,LastName FROM Contact LIMIT 100];

Map<Id,List<Contact>> mapidVsContact = new Map<Id,List<Contact>>();

for(Contact cont: contactList)
    {
        if(mapidVsContact.get(cont.AccountId) == NULL)
             {
                 mapidVsContact.put(cont.AccountId,new List<Contact>());
             }
                 mapidVsContact.get(cont.AccountId).add(cont);
    }

system.debug('---mapidVsContact--->'+mapidVsContact);

Thank you
Ajay Dubedi

All Answers

GauravGargGauravGarg
Sunil,

Please make map like this
Map<Id, List<Contact>> account_ContactMap = new Map<Id, List<Contact>>();

//Loop over Contact records. 
for(Contact con : [SELECT Id, AccountId, Name, Email FROM Contact GroupBy AccountId])
{
	//Cretae new Key for Account, if already not exist. 
	if(!account_ContactMap.containsKey(con.AccountId))
		account_ContactMap.put(con.AccountId, new List<Contact>());

	//Add Contact value for Account key. 
	account_contactMap.get(con.AccountId).add(con);
}



Thanks,

Gaurav
Skype: gaurav62990

Sunil Kumar 545Sunil Kumar 545
Is there any simple code for this requirement..
error:
(Extra ']', at 'AccountId'.)
 
Ajay K DubediAjay K Dubedi
Hi Sunil,

Please refer to the below code. Hope it helps you.
 
List<Contact> contactList = new List<Contact>();
contactList = [SELECT AccountId,LastName FROM Contact LIMIT 100];

Map<Id,List<Contact>> mapidVsContact = new Map<Id,List<Contact>>();

for(Contact cont: contList)
    {
        if(mapidVsContact.get(cont.AccountId) == NULL)
             {
                 mapidVsContact.put(cont.AccountId,new List<Contact>());
             }
                 mapidVsContact.get(cont.AccountId).add(cont);
    }

system.debug('---mapidVsContact--->'+mapidVsContact);
    
Please select as best answer if it helps you.
    
Thank You,
Ajay Dubedi
Sunil Kumar 545Sunil Kumar 545
hi
Ajay Dubedi
i have an error in this code...
(Variable does not exist: contact)
Ajay K DubediAjay K Dubedi
Hi Sunil 

Try this one
List<Contact> contactList = new List<Contact>();
contactList = [SELECT AccountId,LastName FROM Contact LIMIT 100];

Map<Id,List<Contact>> mapidVsContact = new Map<Id,List<Contact>>();

for(Contact cont: contactList)
    {
        if(mapidVsContact.get(cont.AccountId) == NULL)
             {
                 mapidVsContact.put(cont.AccountId,new List<Contact>());
             }
                 mapidVsContact.get(cont.AccountId).add(cont);
    }

system.debug('---mapidVsContact--->'+mapidVsContact);

Thank you
Ajay Dubedi
This was selected as the best answer
Akshay_DhimanAkshay_Dhiman

Hi Sunil,

Please try the below code Open Execute Anonymous and write this code:

 

public map<Id,List<Contact>> accountContactMap = new map<id,List<Contact>>();

List<Account> lstAccount = [SELECT Id, name, (SELECT Id, Name FROM Contacts) FROM Account];

for(Account acc : lstAccount)
{
    accountContactMap.put(acc.id, acc.Contacts);
}

System.debug(accountContactMap);


Please make it best if it helps.

Thanks
Akshay

Sunil Kumar 545Sunil Kumar 545
i have ha doubt in thsi requirement ...
i have code like this ...

public map<Account,List<Contact>> accountContactMap = new map<Account,List<Contact>>();
List<Account> lstAccount = [SELECT Id, name, (SELECT Id, Name FROM Contacts) FROM Account];
for(Account acc : lstAccount)
{
this.accountContactMap.put(acc, acc.Contacts);
}
System.debug(this.accountContactMap);

but its coming an error'// 
Like..
( This cannot be referenced in a static context)...
can anyone tell me?
Ajay K DubediAjay K Dubedi
Hi Sunil,

Please refer to the below code.

public map<Account,List<Contact>> accountContactMap = new map<Account,List<Contact>>();
List<Account> lstAccount = [SELECT Id, name, (SELECT Id, Name FROM Contacts) FROM Account];
for(Account acc : lstAccount)
{
accountContactMap.put(acc, acc.Contacts);
}
System.debug(accountContactMap);

acc is variable which will iterate for all the accounts from the list "lstAccount".So no need to use "this" in this case.

Thank You,
Ajay Dubedi