+ Start a Discussion
Arian Fetahaj 5Arian Fetahaj 5 

Create and Write to 3 Level Map

Hi Everyone.

I am trying to dynamically create a nested Map based on an AggregateResult. The results are grouped by Product, Template and Transaction Type. Therefor I am trying to create a Map like this: Map<String, Map<String, Map<String, Decimal>>>

The first String, Map Combination is Product ID and a Map for all different associated Templates. The second Map contains all associated Templates for that particular Product and a Map with all Transaction Types associated to that particular Template. The Decimal is the actual amount of the transaction. 

I am fairly new to Apex and programming in general so I am still trying to learn a lot. This is what I came up with but based on the research I did over the weekend, it is wrong. I am just not able to translate what I found out to my use-case.
 
//HelperMap
        Map<String, Map<String, Map<String, Decimal>>> cmap = New Map<String, Map<String, Map<String, Decimal>>>();

        //Query
        AggregateResult[] groupedResults = ...
        
        For(AggregateResult ar : groupedResults){

            If(!cmap.containsKey(ar.get('brand'))){
                Map<String, Map<String, Decimal>> templateMap = New Map<String, Map<String, Decimal>>();
                cmap.put(ar.get('brand'), templateMap);
            }

            If(!templateMap.containsKey(ar.get('template'))){
                Map<String, Decimal> insideMap = New Map<String, Decimal>();
                templateMap.put('template', insideMap);
            }

            insideMap.put(ar.get('type'), ar.get('amount'));
            templateMap.put('template', insideMap);
            cmap.put(ar.get('brand'), templateMap);

        }

I censored the query because it contains company information. The 'brand', 'template' and 'type' are aliases. 
Best Answer chosen by Arian Fetahaj 5
Nayana KNayana K
String brand, template, type, amount;
Map<String, Map<String, Map<String, Decimal>>> cmap = New Map<String, Map<String, Map<String, Decimal>>>();

Map<String, Map<String, Decimal>> templateMap;
Map<String, Decimal> insideMap;

For(AggregateResult ar : groupedResults){
brand = (String) ar.get('brand');
template = (String) ar.get('template');
type =(String) ar.get('type');
amount = (Decimal) ar.get('amount');
templateMap = New Map<String, Map<String, Decimal>>();
insideMap = New Map<String, Decimal>();

            If(!cmap.containsKey(brand)){
                insideMap.put(type, amount);
                templateMap.put(template, insideMap);
                cmap.put(brand, templateMap);
            }
            else {
                templateMap = camp.get(brand);
                if(templateMap.containsKey(template)){
                    insideMap = templateMap.get(template);
                    insideMap.put(type, amount);
                }
                else {
                    insideMap.put(type, amount);
                    templateMap.put(template, insideMap);

                }
                
            }

        }
Please give this a try 

All Answers

Nayana KNayana K
String brand, template, type, amount;
Map<String, Map<String, Map<String, Decimal>>> cmap = New Map<String, Map<String, Map<String, Decimal>>>();

Map<String, Map<String, Decimal>> templateMap;
Map<String, Decimal> insideMap;

For(AggregateResult ar : groupedResults){
brand = (String) ar.get('brand');
template = (String) ar.get('template');
type =(String) ar.get('type');
amount = (Decimal) ar.get('amount');
templateMap = New Map<String, Map<String, Decimal>>();
insideMap = New Map<String, Decimal>();

            If(!cmap.containsKey(brand)){
                insideMap.put(type, amount);
                templateMap.put(template, insideMap);
                cmap.put(brand, templateMap);
            }
            else {
                templateMap = camp.get(brand);
                if(templateMap.containsKey(template)){
                    insideMap = templateMap.get(template);
                    insideMap.put(type, amount);
                }
                else {
                    insideMap.put(type, amount);
                    templateMap.put(template, insideMap);

                }
                
            }

        }
Please give this a try 
This was selected as the best answer
Arian Fetahaj 5Arian Fetahaj 5
Hi Nayana, this worked! Thank you so much. Really appreciate it. :)