I have a trigger and a class that is working as intended if records are saved individually. When a record is updated, that record should lookup to the associated account's monthly goal for the correct date of that record. However, it's not working as expected with bulk updates. It seems as if it is taking one account id and using that one accountid in the keybylookupmap for all records. The class is:
Public with sharing class InspectionQuotasHandler{
public static void UpdateMonthlyQuotaOfInspection(List<Inspection__c> InspectionQuotasList){
Set<id> AccountId = new Set<Id>();
for (Inspection__c i : (List<Inspection__c>)Trigger.new) {
AccountId.add(i.Franchise__c);
}
Map<String,map<String,Id>> keyByLookupMap = new Map<String,map<String,Id>>();
for(Monthly_Quota__c mq : [SELECT Id, Name,Account__c
FROM Monthly_Quota__c
WHERE Account__c = :AccountId]){
if(!keyByLookupMap.containsKey(mq.Account__c))
keyByLookupMap.put(mq.Account__c, new map<String,Id>());
keyByLookupMap.get(mq.Account__c).put(mq.Name,mq.Id);
if(!keyByLookupMap.isEmpty()){
for(Inspection__c i : (List<Inspection__c>)trigger.new){
if(i.Inspection_Date__c != null && keyByLookupMap.containsKey(mq.Account__c) && keyByLookupMap.get(mq.Account__c).containsKey(convertMonthToWord(i.Inspection_Date__c.month()))){
if(i.Status__c == 'Completed' && i.Month_Since_Launch__c > 0){
i.Monthly_Quota__c = keyByLookupMap.get(mq.Account__c).get(convertMonthToWord(i.Inspection_Date__c.month()));
}
}
}
}
}
}
public static String convertMonthToWord(Integer monthIndex){
if(monthIndex==1){
return 'January';
}else if(monthIndex==2){
return 'February';
}else if(monthIndex==3){
return 'March';
}else if(monthIndex==4){
return 'April';
}else if(monthIndex==5){
return 'May';
}else if(monthIndex==6){
return 'June';
}else if(monthIndex==7){
return 'July';
}else if(monthIndex==8){
return 'August';
}else if(monthIndex==9){
return 'September';
}else if(monthIndex==10){
return 'October';
}else if(monthIndex==11){
return 'November';
}else if(monthIndex==12){
return 'December';
}else{
return null;
}
}
} Here is the trigger:
trigger InspectionQuotas on Inspection__c (Before update) {
if(trigger.IsBefore && trigger.isUpdate){
InspectionQuotasHandler.UpdateMonthlyQuotaOfInspection(trigger.new); // add trigger.old if required
}
}
How do I make this work as intended for bulk updates?