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
Ron08Ron08 

nested maps

Can someone please help with populating nested maps:

I have 2 maps:
map<id, map<id, string>> map1 = new map<id, map<id, string>>();
map<id, map<id, string>> map2 = new map<id, map<id, string>>();

lets say map1 contains values:
outterid1, innerid1, String1
outterid1, innerid1, String2
outterid1, innerid2, String1
outterid2, innerid1, String1

I need populate map2 using values from map1 as below
outterid1, innerid1, String1;String2
outterid1, innerid2, String1
outterid2, innerid1, String1

Thanks.
karthikeyan perumalkarthikeyan perumal
Hello, 

here is some idea about add values in  nested map.
 
List<String> SourceList = new List<String>{'TestData1','TestData2'};
List<String> innerKeys = new List<String>{'innerOne','innerTwo'};
List<String> values = new List<String>{'valueOne','valueTwo'};

Map<String,Map<String,String>> NestedMAp = new Map<String,Map<String,String>>();
Map<String,String> interMap
for(Integer i = 0; i< SourceList.size(); i++){

interMap = new Map<String,String>();

for(Integer j = 0; j<innerKeys.size(); j++){
interMap.put(innerKeys[j],values[j]);
}

NestedMAp.put(SourceList[i],interMap);

}
hope this will give some idea. 

Thanks
karthik

 
Ajay K DubediAjay K Dubedi
Hi,

You can reference from below link on how to populate nested map :

https://developer.salesforce.com/forums/?id=906F000000092uHIAQ
 
https://www.reddit.com/r/salesforce/comments/6hplul/how_to_populate_data_in_a_3rdlevel_deep_nested_map/
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Deepali KulshresthaDeepali Kulshrestha
Hi Ron08,

Here is a simple process to create nested Map.
// This line creates the map in the proper format
Map<Id,Map<Id,Addendum__c>> addendums = new Map<Id,Map<Id,Addendum__c>>{};

// This for loop goes through each addendum and first places it into a single dimension map.  
// Once that map is created, it is placed into the final multi-dimensional
for(Addendum__c addendum : [SELECT Id,Opportunity__c FROM Addendum__c WHERE Opportunity__c IN :oppBatch])
{
    if(addendums.get(addendum.Opportunity__c) == null)
    {
        addendums.put(addendum.Opportunity__c, new Map<Id, Addendum__c>{addendum.Id => addendum);
    }
    else
    {
        addendums.get(addendum.Opportunity__c).put(addendum.Id, addendum);
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com