0

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?

0

This is because you are not taking Account reference when you are doing mapping.

You first need to find Monthly quota based on Account and then need to use that records.

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(i.Franchise__c) && keyByLookupMap.get(i.Franchise__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(i.Franchise__c).get(convertMonthToWord(i.Inspection_Date__c.month()));
            }
        }            
    }
  • I have edited the class to the following, but now it is not working at all. – Sebastian Feb 21 '18 at 17:13
  • 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); } – Sebastian Feb 21 '18 at 18:41
  • This did not work – Sebastian Feb 21 '18 at 18:41
  • @Sebastian You have added {}. check I never added them and thats why your code was not working. – Tushar Sharma Feb 22 '18 at 13:50
  • @Sebastian when we don't add curly braces if block only consider the next line. In . you case case you have included completed code block inside if thats why it doesn't work. – Tushar Sharma Feb 22 '18 at 15:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.