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
Tony Williams 9Tony Williams 9 

Correctly Searching Map<String,List<Custom Object> over 3100 records

I We are using a Custom object calles US_Counties which includes county name, state, and a list of employee personnel with a max record size of 3,145.
1) Programmatically I want to be able to search the map Collection that stores this info by County name and pull out a list of US Counties objecsts and then based on the Account's Physical state, insert certain personnel into the Account's Account team Object. 
Howvever when I search for the Accont's county name in the Map I get all 3,145 records returned.  Can anyone help me find out what I am doing incorrectly?   

2)  Also how can I use this with a batch script and  avoid the 10000 DML statements Governor Limit?
Thanks
Here is the code:
 
system.debug('<<NUMBER OF QUERIES In TOTAL : '+Limits.getLimitQueries());
        
        //For each account selected go and get the Account Team Mebmers while  updating the account owner.
        List<US_Counties__c> uscs = [Select Name,State_Name__c,Field_Director__c, Work_Group_Leader__c, Primary_Missionary__c from US_Counties__c  ORDER BY State_Name__c, Name Asc];
        Map<String,List<US_Counties__c>> usCounties = new Map<String,List<US_Counties__c>>();
        List<US_Counties__c> all_counties = new List<US_Counties__c>();
        for(US_Counties__c usc : uscs){
            	all_counties.add(usc);
            	usCounties.put(usc.Name,all_counties);
		}
        for(Account a: Accts){
        	system.debug('<<NUMBER OF QUERIES In TOTAL : '+Limits.getLimitQueries());
           	system.debug('<<ACCOUNT>> '+a);
            //Make sure that the Physical Counties are searched on and not Phys state because the returned list size is much less on avg.
            List<US_Counties__c> sampleCty = usCounties.get(a.Physical_County__c);
          	system.debug('<<GET COUNTIES SIZE>> '+sampleCty.size());//displays 3,145 records and not 8 for Orange County
            system.debug('<<GET PHY COUNTY >> '+a.Physical_County__c);
            List<US_Counties__c> countiesFound = new List<US_Counties__c>();
          try{
            	countiesFound = usCounties.get(a.Physical_County__c);
			system.debug('<<GET COUNTIES FOUND>> '+countiesFound);
              	for(US_Counties__c a_county : countiesFound){
                    if(a_county.State_Name__c == a.Physical_State__c){
                    	system.debug('<< NUMBER OF QUERIES USED >> '+ Limits.getQueries());
                		insert_ATM_FieldDirs.add(new AccountTeamMember(UserId=countiesFound[0].Field_Director__c, AccountId=a.Id,TeamMemberRole='Field Director')); 
                		insert_ATM_WGroupLdrs.add(new AccountTeamMember(UserId=countiesFound[0].Work_Group_Leader__c, AccountId=a.Id,TeamMemberRole='Work Group Leader'));
                		insert_ATM_PMissions.add(new AccountTeamMember(UserId=countiesFound[0].Primary_Missionary__c, AccountId=a.Id,TeamMemberRole='Primary Missionary'));
                		ownerIDS.add(countiesFound[0].Primary_Missionary__c);
                        break;
                    }
              	}
            	
            }catch(NullPointerException npex){ // TO test: 1)Create county that can be found or 2) that is mispelled 3) 1st letter not capitalized              
                
                system.debug(npex.getMessage());
                
            }//CATCH
             
    	}//OUT FOR



 
Best Answer chosen by Tony Williams 9
Abhishek BansalAbhishek Bansal
Hi Tony,

Please find the updated code below :
List<US_Counties__c> uscs = [Select Name,State_Name__c,Field_Director__c, Work_Group_Leader__c, Primary_Missionary__c from US_Counties__c  ORDER BY State_Name__c, Name Asc];

Map<String,List<US_Counties__c>> usCounties = new Map<String,List<US_Counties__c>>();

for(US_Counties__c usc : uscs){
	if(!usCounties.containsKey(usc.Name)){
		usCounties.put(usc.Name,new List<US_Counties__c>());
	}
	usCounties.get(usc.Name).add(usc);
}

for(Account a: Accts){
	if(usCounties.containsKey(a.Physical_County__c)){
		for(US_Counties__c a_county : usCounties.get(a.Physical_County__c)){
			if(a_county.State_Name__c == a.Physical_State__c){
				insert_ATM_FieldDirs.add(new AccountTeamMember(UserId=countiesFound[0].Field_Director__c, AccountId=a.Id,TeamMemberRole='Field Director')); 
				insert_ATM_WGroupLdrs.add(new AccountTeamMember(UserId=countiesFound[0].Work_Group_Leader__c, AccountId=a.Id,TeamMemberRole='Work Group Leader'));
				insert_ATM_PMissions.add(new AccountTeamMember(UserId=countiesFound[0].Primary_Missionary__c, AccountId=a.Id,TeamMemberRole='Primary Missionary'));
				ownerIDS.add(countiesFound[0].Primary_Missionary__c);
				break;
			}
		}
	}
}

If you insert your list out of the for loop then you will not hit with any of the DML limits unless you have a large number of data in your org
Please take care if  there is any compilation error and let us know if you still face any issues with this code.

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi Tony,

Please find the updated code below :
List<US_Counties__c> uscs = [Select Name,State_Name__c,Field_Director__c, Work_Group_Leader__c, Primary_Missionary__c from US_Counties__c  ORDER BY State_Name__c, Name Asc];

Map<String,List<US_Counties__c>> usCounties = new Map<String,List<US_Counties__c>>();

for(US_Counties__c usc : uscs){
	if(!usCounties.containsKey(usc.Name)){
		usCounties.put(usc.Name,new List<US_Counties__c>());
	}
	usCounties.get(usc.Name).add(usc);
}

for(Account a: Accts){
	if(usCounties.containsKey(a.Physical_County__c)){
		for(US_Counties__c a_county : usCounties.get(a.Physical_County__c)){
			if(a_county.State_Name__c == a.Physical_State__c){
				insert_ATM_FieldDirs.add(new AccountTeamMember(UserId=countiesFound[0].Field_Director__c, AccountId=a.Id,TeamMemberRole='Field Director')); 
				insert_ATM_WGroupLdrs.add(new AccountTeamMember(UserId=countiesFound[0].Work_Group_Leader__c, AccountId=a.Id,TeamMemberRole='Work Group Leader'));
				insert_ATM_PMissions.add(new AccountTeamMember(UserId=countiesFound[0].Primary_Missionary__c, AccountId=a.Id,TeamMemberRole='Primary Missionary'));
				ownerIDS.add(countiesFound[0].Primary_Missionary__c);
				break;
			}
		}
	}
}

If you insert your list out of the for loop then you will not hit with any of the DML limits unless you have a large number of data in your org
Please take care if  there is any compilation error and let us know if you still face any issues with this code.

Thanks,
Abhishek Bansal.
This was selected as the best answer
Tony Williams 9Tony Williams 9
Thanks Abhishek that was it... I forgot to use conatinskey() method and just used get() method. It runs smoothly.