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
Bavadharani GanesanBavadharani 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).???);
}
 
Best Answer chosen by Bavadharani Ganesan
ravi soniravi soni
Hi Bavadharani,
try  following way
1. //get base on accountId
 if(relatedContactMap.get(con.AccountId)!=null){
        for(contact oCon : relatedContactMap.get(con.AccountId)){
          system.debug('oCon : '+oCon.MailingCity);
        }
    }

2. //get only values
for(list<contact> lstCon : relatedContactMap.values()){
    for(contact con : lstCon){
          system.debug('con : '+con.MailingCity);
    }
  
}
let me know if it helps you and close your query by marking it as best Answer.
Thank you
 

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi,

List< Contact> clist =relatedContactMap.get(con.AccountId)    --> this will return list of Contact.
you need to iterate it further.
 
for(Conatct c :clist)
{
System.debug('******' + c.MailingCity);
}

If it helps ,please mark it as best Answer.

Thanks!
Suraj Tripathi 47Suraj Tripathi 47

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

for(Integer i=0;i<relatedContactMap.get(con.AccountId).size();i++){
system.debug(relatedContactMap.get(con.AccountId)[i].MailingCountry );
}
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
 

 

ravi soniravi soni
Hi Bavadharani,
try  following way
1. //get base on accountId
 if(relatedContactMap.get(con.AccountId)!=null){
        for(contact oCon : relatedContactMap.get(con.AccountId)){
          system.debug('oCon : '+oCon.MailingCity);
        }
    }

2. //get only values
for(list<contact> lstCon : relatedContactMap.values()){
    for(contact con : lstCon){
          system.debug('con : '+con.MailingCity);
    }
  
}
let me know if it helps you and close your query by marking it as best Answer.
Thank you
 
This was selected as the best answer