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
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);
}
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);
}
First you will have to initialise the map and then it's nested map for each account record ID. See the following example - Now, you will check whether map has the account key or not. If account key doesn't exist then need to intialise nested map 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
Complete example -
I hope it makes sense to you!
All Answers
First you will have to initialise the map and then it's nested map for each account record ID. See the following example - Now, you will check whether map has the account key or not. If account key doesn't exist then need to intialise nested map 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
Complete example -
I hope it makes sense to you!
Thanks for explaining the use of it. It is helping.
Thanks,
Beenal