0

Can some one give an example of how to populate data in to:

Map<Id,Map<Id, List<Contact>>>
1
  • Kindly specify the entire requirement. Both the IDs belong to which object and how you want to Map the data Oct 27 '17 at 9:55
2

You should always provide an attempted effort when seeking assistance.

The following code will give you some insight into how you can populate nested maps. Unsure as to which SObject Ids you want to use as the keySet() for both maps, I have simply used Account.

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

for (Account acc : [select id, (select id from Contacts) from Account limit 1]) {
    Map<Id, List<Contact>> innerMap = new Map<Id, List<Contact>>();
    innerMap.put(acc.Id, acc.Contacts);
    strangeMap.put(acc.Id, innerMap);
}
System.debug(strangeMap);

A Limit has been applied to my SOQL for testing purposes. You will need to modify per your need.

Also, I wish to point out a scenario to be aware of. In the above SOQL for loop, a relationship query is performed. These are quite handy, but when attempting to access the child records, beware of System limits such as

QueryException such as "Aggregate query has too many rows for direct assignment, use FOR loop"

1

Its very simple. You first need to fill the internal Map and then using that Map you need to fill outer Map.

Map<Id,Map<Id, List<Contact>>> accconmap = new Map<Id,Map<Id, List<Contact>>>();
//Here accList is list of accounts records
for(Account acc : accList) {
   //if you want to append in existing map or there re chances that you will get same key multiple time then we use this.
   if(!accconmap.containsKey(acc.Id))
        accconmap.put(acc.Id, new Map<Id, List<Contact>>());

    //create internal map and fill it
    Map<Id, List<Contact>> conmap = new Map<Id, List<Contact>>();
    conmap .put(acc.Id, acc.contacts);
    accconmap.put(acc.Id, conmap );
    //you can also do this
    //accconmap.get(acc.Id).get(acc.Id).addAll( acc.contacts );
    // don't forget to add null check here

}
0

Here is the code..

//Step1 : Fill the key with account id in this case 
Map<Id,Map<Id, List<Contact>>> awesomeAccMap = new Map<Id,Map<Id, List<Contact>>>();

//iterate over list of Account records to fill account id in outermap
for(Account acc : [select id, (select id from Contacts) from Account limit 1]) {
   if(!awesomeAccMap.containsKey(acc.Id))
        awesomeAccMap.put(acc.Id, new Map<Id, List<Contact>>());

//Step2 : Fill the value with Accountid and contact records
    //create internal contact map and fill it
    Map<Id, List<Contact>> awesomeConMap = new Map<Id, List<Contact>>();
    awesomeConMap .put(acc.Id, acc.contacts);

    //fill outer account map
    awesomeAccMap.put(acc.Id, awesomeConMap);

    //get accountmap
    system.debug('awesomeAccMap'+ awesomeAccMap);
    //get contactmap
    system.debug('awesomeConMap'+ awesomeConMap);
}
Output for above query for more clearity:
awesomeAccMap{0012h00000FBx99AAX={0012h00000FBx99AAX=(Contact:{AccountId=0012h00000FBx99AAX, Id=0032h00000E6zW5AAX, RecordTypeId=0121N000001M3RCQAX})}}

awesomeConMap{0012h00000FBx99AAX=(Contact:{AccountId=0012h00000FBx99AAX, Id=0032h00000E6zW5AAX, RecordTypeId=0121N000001M3RCQAX})}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.