+ Start a Discussion
KimKim 

Checking if nested map contains key

Hello,

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 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,
if (ratMap.containsKey('Access')) {
even though the LOB value for that rat was not 'Access'?

 
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') {
				System.debug('Getting this FAR 0 --> ' + ratMap);
				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;
				}

 
Paul S.Paul S.
What is the "request" record/object show in the above code?  Is that a record whose field values you are trying to compare to the map you've built?