Skip to main content
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);

}

}

 
2 answers
  1. Oct 28, 2020, 5:50 PM
    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.
0/9000