1
List<QuoteLineItem> childQLIs = [Select id, category__c, Parent_Quote__c, Product2Id, PricebookEntryId, Quantity, Product2Id From QuoteLineItem]; 

 Map<Id,Map<String,List<QuoteLineItem>>> OuterMap = New Map<Id,Map<String,List<QuoteLineItem>>>();

 Map<String,List<QuoteLineItem>> InnerMap = New Map<String,List<QuoteLineItem>>();

        for(QuoteLineItem childQLI: childQLIs) {
            if(InnerMap.containsKey(childQLI.category__c){
                InnerMap.get(childQLI.category__c).add(childQLI);
            } 
            else {
                InnerMap.put(childQLI.category__c, new List<QuoteLineItem> { childQLI });
            }
        }

Now my requirement is to wrap this map further with the Parent_Quote__c field. Trying to achieve a map--

Map<Id,Map<String,List<QuoteLineItem>>> OuterMap and need to do OuterMap.put(Parent_Quote__c, InnerMap);
  • 1
    Consider just using a separate wrapper class or classes to build out something of this complexity. In general, anytime you're nesting a Map within a Map, consider wrapper classes instead – Brian Miller Dec 18 '19 at 14:01
  • Thanks Brian, Any pointers for class for this scenario? – shukla yogesh Dec 18 '19 at 14:07
1

Declare innermap within for loop and assign it to either an existing entry in outer map or new map if not already existing.

List<QuoteLineItem> childQLIs = [Select id, category__c, Parent_Quote__c, Product2Id, PricebookEntryId, Quantity, Product2Id From QuoteLineItem]; 

 Map<Id,Map<String,List<QuoteLineItem>>> OuterMap = New Map<Id,Map<String,List<QuoteLineItem>>>();



        for(QuoteLineItem childQLI: childQLIs) {
             Map<String,List<QuoteLineItem>> InnerMap;
            if(OuterMap.containsKey(childQLI.Parent_Quote__c)){
                 innerMap = OuterMap.get(childQLI.parent_Quote__c);
            }else
                 innerMap = New Map<String,List<QuoteLineItem>>();
            }
            if(InnerMap.containsKey(childQLI.category__c){
                InnerMap.get(childQLI.category__c).add(childQLI);
            } 
            else {
                InnerMap.put(childQLI.category__c, new List<QuoteLineItem> { childQLI });
            }
            OuterMap.put(childQLI.Parent_Quote__c, innerMap);
        }
| improve this answer | |

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.