You need to sign in to do that
Don't have an account?
Bavadharani Ganesan
How to get values from nested map Map<Id,List<Contact>> ?
Hi,
In the below code, how shall I get the 'MailingCity' of contact from the relatedContactMap.
Map<Id,List<Contact>> relatedContactMap = new map<Id,List<Contact>>();
List<Contact> relatedContactList = new List<Contact>();
for(Contact con: [SELECT Id,AccountId, MailingCity, MailingStreet, MailingState,
MailingPostalCode, MailingCountry FROM Contact WHERE
AccountId ='0015Y00002m8cuE' AND ID != '0035Y00003wkUi4'])
//Here the accountId is same for all 13 contacts. so we have check containskey. otherwise only one contact will be added to this map since the key should be unique.
{
if(!relatedContactMap.containskey(con.AccountId))
{
relatedContactMap.put(con.AccountId, new List<contact>{con});
}
else
{
relatedContactList = relatedContactMap.get(con.AccountId);
relatedContactList.add(con);
relatedContactMap.put(con.AccountId,relatedContactList);
}
}
system.debug('relatedContactMap '+relatedContactList.size());
for(contact con : [SELECT Id,AccountId, MailingCity, MailingStreet, MailingState,
MailingPostalCode, MailingCountry FROM Contact WHERE
ID = '0035Y00003wkUi4'])
{
system.debug('relatedContactMap.get(con.AccountId) '+relatedContactMap.get(con.AccountId).???);
}
In the below code, how shall I get the 'MailingCity' of contact from the relatedContactMap.
Map<Id,List<Contact>> relatedContactMap = new map<Id,List<Contact>>();
List<Contact> relatedContactList = new List<Contact>();
for(Contact con: [SELECT Id,AccountId, MailingCity, MailingStreet, MailingState,
MailingPostalCode, MailingCountry FROM Contact WHERE
AccountId ='0015Y00002m8cuE' AND ID != '0035Y00003wkUi4'])
//Here the accountId is same for all 13 contacts. so we have check containskey. otherwise only one contact will be added to this map since the key should be unique.
{
if(!relatedContactMap.containskey(con.AccountId))
{
relatedContactMap.put(con.AccountId, new List<contact>{con});
}
else
{
relatedContactList = relatedContactMap.get(con.AccountId);
relatedContactList.add(con);
relatedContactMap.put(con.AccountId,relatedContactList);
}
}
system.debug('relatedContactMap '+relatedContactList.size());
for(contact con : [SELECT Id,AccountId, MailingCity, MailingStreet, MailingState,
MailingPostalCode, MailingCountry FROM Contact WHERE
ID = '0035Y00003wkUi4'])
{
system.debug('relatedContactMap.get(con.AccountId) '+relatedContactMap.get(con.AccountId).???);
}
try following way let me know if it helps you and close your query by marking it as best Answer.
Thank you
All Answers
List< Contact> clist =relatedContactMap.get(con.AccountId) --> this will return list of Contact.
you need to iterate it further.
If it helps ,please mark it as best Answer.
Thanks!
Hi Bavadharani Ganesan,
Please find the solution.
relatedContactMap.get(con.AccountId) this will give List of contacts
so you can get values from nested map Map<Id,List<Contact>> as the below
Please let me know it is working or not.Please mark it as the Best Answer so that other people would take reference from it.
Thank You
try following way let me know if it helps you and close your query by marking it as best Answer.
Thank you