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
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.