Hi I have a map which has a map inside a map as below

    Map<String,Map<String,String>> finaldataelementmap=New Map<String,Map<String,String>>();
    Map<String,String> tempmap = new Map<String,String>();
    finaldataelementmap.put(Id, tempmap);

   // The tempmap has the list of values as below
 //   (AcccountId = xxxx, Amount = xxxx, Tracking = xxxx, Mode = xxxx, Value = xxxx)

Now I want to iterate over finaldataelement map and get accountid, amount,tracking, mode values to store in different variables.

My updated code based on the suggestion below

List<Invoice__c> invlst = [select id,Account__c from Invoice__c where Account__c IN :mapAccount.keyset()];            
                      List<OrderApi__Invoice__c > invids = new List<Invoice__c>();           
                        system.debug('Before final opp' +finaldataelementmap);
system.debug('Final is' +finaldataelementmap.keySet());
                             for(Invoice__c inv : invlst){
                            for (String s : finaldataelementmap.keySet()) {

              Map<String, String> innerMap = finaldataelementmap.get(s);
    system.debug('Inner map is ' +innerMap);
                                     Receipt__c rc = new Receipt__c();

    rc.Amount = innermap.get('Amount');
                                        rc.Type__c = innermap.get('Mode');

When I print the finaldataelementmap, it gives me two key value pair sets 1. key 500 , value - list as above 2. key 650 , value - gives the value as "Already Output instead of printing values

 19:21:04.809 (3450018573)|USER_DEBUG|[472]|DEBUG|Before final oop {629={Amount=5100.0000, Type=, Mode=Check, ...}, 630=(already output)}

19:21:04.809 (3450286435)|USER_DEBUG|[474]|DEBUG|Final is {Amount=5100.0000, Type=,Mode=Check, ...}

19:21:04.809 (3450413947)|USER_DEBUG|[476]|DEBUG|Innermap is {Amount=5100.0000,Type=,Mode=Check, ...}
19:21:04.809 (3451144354)|USER_DEBUG|[481]|DEBUG|Amount is 5100.00
19:21:04.809 (3451208808)|USER_DEBUG|[482]|DEBUG|tracking isC10426
19:21:04.809 (3452343632)|USER_DEBUG|[481]|DEBUG|Amount is 5100.00
19:21:04.809 (3452372836)|USER_DEBUG|[482]|DEBUG|tracking isC10426
19:21:04.809 (3452771535)|USER_DEBUG|[526]|DEBUG|Size of receipts is2
19:21:04.809 (3452343632)|USER_DEBUG|[481]|DEBUG|Amount is 5100.00
19:21:04.809 (3452372836)|USER_DEBUG|[482]|DEBUG|tracking isC10426
19:21:04.809 (3452771535)|USER_DEBUG|[526]|DEBUG|Size of receipts is4
19:21:04.809 (3452343632)|USER_DEBUG|[481]|DEBUG|Amount is 5100.00
19:21:04.809 (3452372836)|USER_DEBUG|[482]|DEBUG|tracking isC10426
19:21:04.809 (3452771535)|USER_DEBUG|[526]|DEBUG|Size of receipts is6

Now, Instead of iterating over values of both 629 and 630 , which has two different amounts, the loop is iterating over only key 629, multiple times. I suppose this is because when I print the finaldataelementmap, I see that the value for 630 is listed as "already output" instead of actual value

  • What specific issue did you encounter? Did you receive an error message? What is the rest of your code inside the for loop? (Please edit to add information).
    – David Reed
    Jan 6 '20 at 0:42
  • Updated my question
    – user64977
    Jan 6 '20 at 0:44

You don't need nested iteration here. There's no reason to iterate over the keys of the inner Map. While you could make that work, you'd end up with a complex if/else or switch on statement inside the inner for loop.

It's much easier to directly access the inner Map keys whose values you require. It'd look something like this:

for (String s : finaldataelementmap.keySet()) {
    Map<String, String> innerMap = finaldataelementmap.get(s);

    Receipt__c r = new Receipt__c();
    r.Amount__c = innerMap.get('Amount');
    r.Tracking__c = innerMap.get('Tracking');
    // and so on. 

Second Problem

Your Map finaldataelementmap contains two references to the same child Map under different keys. The code creating that Map has a bug in it - usually, this happens when you forget to create a new object instance in each iteration of a loop that's generating a nested data structure.

  • I did try that but I am having the same issue. Added the log for finaldataelementmap in the question above
    – user64977
    Jan 6 '20 at 1:27
  • I'm sorry, @user64977, but I really have no idea what you're asking. You're showing debug output, but there are no debug statements in the code you've shown. What, specifically, does not work? What is the behavior? What error do you see?
    – David Reed
    Jan 6 '20 at 1:50
  • I did add the debug statements in the code and the complete statement. I am getting the "already output" in the map value instead of actual values.
    – user64977
    Jan 6 '20 at 2:01

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.