0

I need a some feedback/guidance with nested maps. Business request: Pull a list of records from our custom object "RAT" and do a comparison to check for the "Role" & "LOB" of each rat record.

I have constructed a nested map where my 1st key = LOB, and my 2nd key = Role, and found that my containskey check isn't finding the correct Rat record with the correct 1st key (LOB), but it matches my 2nd key (Role) value. Any idea why it is passing through the if check below even though the LOB value does not match/meet that criteria?

if (ratMap.containsKey('Access')) {

Full class:

public static RatSearchResults ratReturned(RatSearchRequest request) {

    //List<Account_Team_Role__c> rats = new List<Account_Team_Role__c> ();
    Map<String, Map<String, List<Account_Team_Role__c>>> ratMap = new Map<String, Map<String, List<Account_Team_Role__c>>> (); //1st key = LOB, 2nd key = Role
    Map<String, List<Account_Team_Role__c>> ratRoleMap = new Map<String, List<Account_Team_Role__c>> (); //Key = Role 
    Map<String, List<Account_Team_Role__c>> intlRat = new Map<String, List<Account_Team_Role__c>> (); //Key = LOB
    Id automationUserId = '00530000001pyWZAAY';
    Id returnNARatId;


    List<Account_Team_Role__c> ratQuery = [SELECT Id, Role__c, LOB__c, User__c, Product_Specialty__c, Active__c, Active_User__c FROM Account_Team_Role__c
                                           WHERE Active_User__c = True AND Active__c = True AND Account__c = :request.AccountId];

    for (Account_Team_Role__c r : ratQuery) {

        if (request.Market == 'NA') {

            if (ratMap.containsKey(r.LOB__c)) {

                ratRoleMap = ratMap.get(r.LOB__c);

                if (ratRoleMap.containsKey(r.Role__c)) {
                    ratRoleMap.get(r.Role__c).add(r);

                } else {
                    ratRoleMap.put(r.Role__c, new List<Account_Team_Role__c> { r });

                }

            }
            //If the ratMap doesn't contain the LOB key 
            else {
                //Map<String, List<Account_Team_Role__c>> ratRoleMap = new Map<String, List<Account_Team_Role__c>> ();
                ratRoleMap.put(r.Role__c, new List<Account_Team_Role__c> { r });
                ratMap.put(r.LOB__c, ratRoleMap);

            }
        } else if (request.Market != 'NA') {

            if (intlRat.containsKey(r.LOB__c)) {
                intlRat.get(r.LOB__c).add(r);
            } else {
                intlRat.put(r.LOB__c, new List<Account_Team_Role__c> { r });
            }
        }
    }



    if (request.Market == 'NA') {

        if (ratMap.containsKey('Access')) {

            if (ratMap.get('Access').containsKey('Majors')) {

                returnNARatId = ratMap.get('Access').get('Majors') [0].User__c;

            } else if (ratMap.get('Access').containsKey('BDR')) {

                returnNARatId = ratMap.get('Access').get('BDR') [0].User__c;

            } else if (ratMap.get('Access').containsKey('Hunter')) {

                if (ratMap.get('Access').get('Hunter') [0].Product_Specialty__c == request.OppLProduct) {
                    returnNARatId = ratMap.get('Access').get('Hunter') [0].User__c;
                }

            } else if (ratMap.get('Access').containsKey('Account Manager')) {
                if (ratMap.get('Access').get('Account Manager') [0].Product_Specialty__c == request.OppLProduct) {
                    returnNARatId = ratMap.get('Access').get('Account Manager') [0].User__c;
                }

            } else {
                returnNARatId = automationUserId;
            }
  • Map keys are case-sensitive, have you checked that? Also have you checked for extra trailing spaces in the field values you are using to generate the map keys? – David Cheng Mar 9 '18 at 17:02
  • I found the issue. I had my inner map outside the for loop so it wasn't refreshed. I moved " Map<String, List<Account_Team_Role__c>> ratRoleMap = new Map<String, List<Account_Team_Role__c>> (); //Key = Role " inside the for loop and that worked. – Kim Mar 9 '18 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.