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
SecondID PatelSecondID Patel 

Hi all, I am in the journey of learning apex...Can anyone please explain how to use Map<Id,Map<Id,Sobject>>...I also want to know how to use containsKey method, Get and put method in this Map inside map

Best Answer chosen by SecondID Patel
Naval Sharma4Naval Sharma4
It depends on how you want to write your logic. Basically, I would use Map<Id,Map<Id,Sobject>> to associate contacts and opportunities to an account ID.

First you will have to initialise the map and then it's nested map for each account record ID. See the following example -
Map<Id,Map<Id,Sobject>> mapAccountChildren = new Map<Id,Map<Id,Sobject>>();
Now, you will check whether map has the account key or not. If account key doesn't exist then need to intialise nested map 
if(!mapAccountChildren.containsKey(con.AccountId)){
    mapAccountChildren.put(con.AccountId, new Map<Id, Sobject>());
}
Now, we need to add the Sobject records in the nested map for each account so we will have to use get method and put method 
Get method - Gives you the corresponding value for the passed key 
Put method - Set the value for a given key, look at the above example 

Put the Sobject for a given account 
mapAccountChildren.get(acc.Id).put(con.Id, con);


Complete example -
Map<Id,Map<Id,Sobject>> mapAccountChildren = new Map<Id,Map<Id,Sobject>>();
for(Contact con : [SELECT Id, FirstName, LastName, AccountId FROM Contact WHERE AccountId != NULL]){
   if(!mapAccountChildren.containsKey(con.AccountId)){
      mapAccountChildren.put(con.AccountId, new Map<Id, Sobject>());
   }
   mapAccountChildren.get(con.AccountId).put(con.Id, con);
}


I hope it makes sense to you!


 

All Answers

Naval Sharma4Naval Sharma4
It depends on how you want to write your logic. Basically, I would use Map<Id,Map<Id,Sobject>> to associate contacts and opportunities to an account ID.

First you will have to initialise the map and then it's nested map for each account record ID. See the following example -
Map<Id,Map<Id,Sobject>> mapAccountChildren = new Map<Id,Map<Id,Sobject>>();
Now, you will check whether map has the account key or not. If account key doesn't exist then need to intialise nested map 
if(!mapAccountChildren.containsKey(con.AccountId)){
    mapAccountChildren.put(con.AccountId, new Map<Id, Sobject>());
}
Now, we need to add the Sobject records in the nested map for each account so we will have to use get method and put method 
Get method - Gives you the corresponding value for the passed key 
Put method - Set the value for a given key, look at the above example 

Put the Sobject for a given account 
mapAccountChildren.get(acc.Id).put(con.Id, con);


Complete example -
Map<Id,Map<Id,Sobject>> mapAccountChildren = new Map<Id,Map<Id,Sobject>>();
for(Contact con : [SELECT Id, FirstName, LastName, AccountId FROM Contact WHERE AccountId != NULL]){
   if(!mapAccountChildren.containsKey(con.AccountId)){
      mapAccountChildren.put(con.AccountId, new Map<Id, Sobject>());
   }
   mapAccountChildren.get(con.AccountId).put(con.Id, con);
}


I hope it makes sense to you!


 
This was selected as the best answer
SecondID PatelSecondID Patel
Hi Naval,

Thanks for explaining the use of it. It is helping.

Thanks,
Beenal