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
Dave ShulmanDave Shulman 

Working with Map<String, Map<>>

Hi All,

Im trying to work with multi layer maps and have never done so before.  

What i want to do is get Category as top KeySet then for each category a Contract Name, then for each contract name Value of Sales.  

Im iterating through sales for each Account and trying to add them to the maps or purge and replace to add $$ to the Value of Sales.

it doesnt seem to be working and i cant figure why.  Please advise thanks!
Map<String,Map<String,Decimal>> CategoryList = new Map<String,Map<String,Decimal>>();
    Map<String,Decimal> SubGroup = new Map<String,Decimal>();
    Map<String,Map<String,Decimal>> TierList = new Map<String,Map<String,Decimal>>();
    Map<String,Decimal> TierSubGroup = new Map<String,Decimal>();
    

if(!CategoryList.keyset().contains(RelatedSale.Product_Group__c)){
                                SubGroup.put(RelatedSale.Related_Contract__r.External_Contract_Name__c, RelatedSale.Amount__c);
                                CategoryList.put(RelatedSale.Product_Group__c, SubGroup);
                                SubGroup.clear();
                            }
                            if(!TierList.keySet().contains(RelatedSale.Product_Group__c)) {
                            	TierSubGroup.put(RelatedSale.Related_Contract__r.Contract_Tier__c, RelatedSale.Amount__c);
                                TierList.put(RelatedSale.Product_Group__c, TierSubGroup);
                                TierSubGroup.clear();
                            }
                            if(CategoryList.keyset().contains(RelatedSale.Product_Group__c)) {
                                if(CategoryList.get(RelatedSale.Product_Group__c).keyset().contains(RelatedSale.Related_Contract__r.External_Contract_Name__c)) {
                                	UpdateAmount = CategoryList.get(RelatedSale.Product_Group__c).get(RelatedSale.Related_Contract__r.External_Contract_Name__c) + RelatedSale.Amount__c;
                                    CategoryList.get(RelatedSale.Product_Group__c).remove(RelatedSale.Related_Contract__r.External_Contract_Name__c);
                                    CategoryList.get(RelatedSale.Product_Group__c).put(RelatedSale.Related_Contract__r.External_Contract_Name__c, UpdateAmount);
                                }
                                if(!CategoryList.get(RelatedSale.Product_Group__c).keyset().contains(RelatedSale.Related_Contract__r.External_Contract_Name__c)) {
                                    CategoryList.get(RelatedSale.Product_Group__c).put(RelatedSale.Related_Contract__r.External_Contract_Name__c, RelatedSale.Amount__c);
                                }
                            }
                            if(TierList.keyset().contains(RelatedSale.Product_Group__c)) {
                            	if(TierList.get(RelatedSale.Product_Group__c).keyset().contains(RelatedSale.Related_Contract__r.Contract_Tier__c)) {
                                	UpdateAmount = TierList.get(RelatedSale.Product_Group__c).get(RelatedSale.Related_Contract__r.Contract_Tier__c) + RelatedSale.Amount__c;
                                    TierList.get(RelatedSale.Product_Group__c).remove(RelatedSale.Related_Contract__r.Contract_Tier__c);
                                    TierList.get(RelatedSale.Product_Group__c).put(RelatedSale.Related_Contract__r.Contract_Tier__c, UpdateAmount);
                                }
                                if(!TierList.get(RelatedSale.Product_Group__c).keyset().contains(RelatedSale.Related_Contract__r.Contract_Tier__c)) {
                                    TierList.get(RelatedSale.Product_Group__c).put(RelatedSale.Related_Contract__r.Contract_Tier__c, RelatedSale.Amount__c);
                                }	   
                            }

 
Saurabh Rawat 14Saurabh Rawat 14
What you are trying to achieve is possible through Map, but you have to first think of the structure that your data is going to be.
For ex:-
if you are want data for category vs Contract Name,
then you have to implement Map<String,List<Contract>>
but now you want it in more depth, that for each Contract if you need list of sale, then you have to append like

Map< Category , List< Map<Contract, List<SalesValue> > > >

for data type:-

Map< String, List<Map<Contract, List<Double> > > >

Hope, this will give you an idea, for further query, feel free to ping me.
Dave ShulmanDave Shulman
Hi Saurabh,

the structure i was going with was Map<String (Category),Map<String (contract Name),Decimal (amount)>>

and for each Sale line item im iterating through, seeing if

map.keyset().contains(Sale.Category);

if it doesnt then ill add (Sale.Category, (Sale.ContractName, Amount))

if it does then ill see if 

Map.get(Sale.Category).keyset().contains(Sale.ContractName);

if it has it then 

UpdateAmt = Sale.Amount + Map.get(Sale.Category).get(Sale.ContractName);

remove the value pair:   Map.get(Sale.Category).remove(Sale.ContractName);

and add back with new value:  Map.get(Sale.Category).put(Sale.ContractName, UpdateAmt)


then for each Category im going to grab the ContractName with the highest value in a different method.  However the whole thing is giving me incorrect answers.  Please tell me if im working with my data properly. Thanks

highest value method: 

private static String primaryContracts (Map<String,Decimal> InputMap) {
        String Primary;
        Decimal MaxVar = 0;
        for(String entityID : InputMap.keySet()){
            if(InputMap.get(entityID) > 0 && InputMap.get(entityID) > MaxVar){
                Primary = entityID;
                MaxVar = InputMap.get(entityID);
            }
        }
        return Primary;
    }