0

I have a custom object called Inspection__c, which is a child object of the Account object. The Account object has another child object called Monthly_Quota__c, each Account has 12 records of this (one for each month of the year, each with the same month name across accounts) and Inspection__c has a lookup field that references this object. I would like a trigger on the Inspection__c object that updates the lookup by referencing the name of the Monthly Quota (i.e., January, February, etc.) on the correct account based on the date of Inspection. So for example of Account A does an inspection in February 2018, the lookup will find the 'February' monthly quota for Account A, and if Account B does an inspection in February 2018, that inspection lookup will find the 'February' monthly quota for Account B, etc. etc. The trigger I have so far is:

trigger InspectionQuotas on Inspection__c (after update) {

Set<id> AccountId = new Set<Id>();

for (Inspection__c i : Trigger.new) {
    AccountId.add(i.Franchise__c);
}

Set<String> nameString = new Set<String>{'January','February','March'};

Map<String,Monthly_Quota__c> keyByLookupMap = new Map<String,Monthly_Quota__c>();

for(Monthly_Quota__c mq : [SELECT Id, Name 
                           FROM Monthly_Quota__c 
                           WHERE Name IN :nameString AND Account__c = :AccountId]){
    keyByLookupMap.put(mq.name,mq);
}

for(Inspection__c i : trigger.new){
    if(i.Inspection_Year_Month__c == '2018-01-January'){
        if(keyByLookupMap.containsKey('January'))
        i.Monthly_Quota__c = keyByLookupMap.get('January');
    }
    if(i.Inspection_Year_Month__c == '2018-02-February'){
        if(keyByLookupMap.containsKey('February'))
        i.Monthly_Quota__c = keyByLookupMap.get('February');
    }
    if(i.Inspection_Year_Month__c == '2018-03-March'){
        if(keyByLookupMap.containsKey('March'))
        i.Monthly_Quota__c = keyByLookupMap.get('March');
    }
}

}

But it's not working. What am I doing wrong?

1
  • Whats the data type of Inspection_Year_Month__c ? Check your if blocks whether the code enters there. I would suggest putting a debug statement - System.debug(i.Inspection_Year_Month__c ); . System.debug( datetime.now()); =>2017-12-04 22:03:14 and System.debug(Date.today()); => 2017-12-04 00:00:00
    – SfdcBat
    Dec 4, 2017 at 22:00

2 Answers 2

0

Assuming there is a Date field type on Inspections object. Here is an idea to get you started on what you can do. First thing clean up your trigger and use trigger context https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm . Use Before Update for this case. Use Handler class to put all the logic of the trigger.

   trigger InspectionQuotas on Inspection__c (Before update) {

        if(trigger.IsBefore && trigger.isUpdate){
           InspectionQuotasHandler.UpdateMonthlyQuotaOfInspection(trigger.new); // add trigger.old if required
        }

    }

InspectionQuotasHandler Class( Contains the logic). https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices

Public Class with Sharing InspectionQuotasHandler{

    public static void UpdateMonthlyQuotaOfInspection(List<Inspection__c> InspectionQuotasList){
        Set<id> AccountId = new Set<Id>();

        for (Inspection__c i : Trigger.new) {
            AccountId.add(i.Franchise__c);
        }

        Map<String,Id> keyByLookupMap = new Map<String,Id>();

        for(Monthly_Quota__c mq : [SELECT Id, Name 
                                   FROM Monthly_Quota__c 
                                   WHERE Account__c = :AccountId]){
            keyByLookupMap.put(mq.name,mq.Id);
        }

        if(!keyByLookupMap.isEmpty()){
            for(Inspection__c i : trigger.new){
                if(keyByLookupMap.containsKey(convertMonthToWord(i.Inspection_Year_Month__c.month())){
                    i.Monthly_Quota__c = keyByLookupMap.get(convertMonthToWord(i.Inspection_Year_Month__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;
            }

        }
}
0
0
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,Id> keyByLookupMap = new Map<String,Id>();

    for(Monthly_Quota__c mq : [SELECT Id, Name 
                               FROM Monthly_Quota__c 
                               WHERE Account__c = :AccountId]){
        keyByLookupMap.put(mq.name,mq.Id);
    }

    if(!keyByLookupMap.isEmpty()){
        for(Inspection__c i : (List<Inspection__c>)trigger.new){
            if(keyByLookupMap.containsKey(convertMonthToWord(i.Inspection_Date__c.month()))){
                if(i.Status__c == 'completed' && i.Month_Since_Launch__c > 0){
                i.Monthly_Quota__c = keyByLookupMap.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;
        }

    }

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .