• Arian Fetahaj 5
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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. 
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.