0

I have a trigger which calculate the difference between the "start time" on two child records(Event log records).

The trigger fires on the "after insert" and "after update" of the event log (Custom object) record and then the trigger performs three calculations.

  1. Calculate the difference Status Start Time difference between the event log record with status "New" and Event log record with Status "Work order created" and save the value in NewToCreatedTotalHours__c

  2. Calculate the difference Status Start Time difference between the event log record with status "New" and Event log record with Status "Work order In progress" and save the value in New_to_In_progress__c

  3. Calculate the difference Status Start Time difference between the event log record with status "New" and Event log record with Status "Work order Completed" and saved the value in NewToCompletedHotalHours__c

The first and last pieces are working as expected but the time Calculation between the event log record with status "New" and Event log record with Status "Work order In progress" is not working properly. ZERO is populated on the New_to_In_progress__c field whenever we the trigger fires after insert of the event log with status "Work order In progress".

Here is the trigger logic.

trigger EventLogTrigger on sm1e__Event_Log__c (after insert, after update) {

    Long newHours;
    Long otherHours; 
    Long milliSeconds;
    Double totalMinutes = 0, totalHours = 0;
    Long seconds;
    DateTime startTime;
    Set<String> operationalEventIdSet = new Set<String>();
    sm1e__Operational_Event__c operationalEventInstance = null;
    List<sm1e__Operational_Event__c> operationalEventList = new List<sm1e__Operational_Event__c>();

    for(sm1e__Event_Log__c eventLog : Trigger.new){
       operationalEventIdSet.add(eventLog.sm1e__Operational_Event__c);     
    }

    Map<Id, sm1e__Operational_Event__c> operationalEventMap = new Map<Id, sm1e__Operational_Event__c>();
    operationalEventMap = new Map<Id, sm1e__Operational_Event__c>([SELECT Id, 
                                                                          sm1e__Status__c, 
                                                                          sm1e__Start_Time__c, 
                                                                          (SELECT Id, 
                                                                                  Status_Start_Time__c, 
                                                                                  sm1e__Operational_Event_Status__c 
                                                                           FROM sm1e__Event_Logs__r 
                                                                           ORDER BY CreatedDate ASC) 
                                                                   FROM sm1e__Operational_Event__c WHERE Id IN: operationalEventIdSet]);

    for(sm1e__Event_Log__c eventLog : Trigger.new){
        if(operationalEventMap != null && operationalEventMap.containsKey(eventLog.sm1e__Operational_Event__c)){
            if(eventLog.sm1e__Operational_Event_Status__c == 'Work Order Created'){
               totalMinutes = 0;
               totalHours = 0;
               startTime = operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Start_Time__c;
               operationalEventInstance = new sm1e__Operational_Event__c();
               operationalEventInstance.Id = eventLog.sm1e__Operational_Event__c;
               for(sm1e__Event_Log__c  eLog : operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Event_Logs__r){
                   if(eLog.sm1e__Operational_Event_Status__c == 'New' && eLog.Status_Start_Time__c != null)
                      newHours = eLog.Status_Start_Time__c.getTime();
                   if(eLog.sm1e__Operational_Event_Status__c == 'Work Order Created' && eLog.Status_Start_Time__c != null)
                      otherHours = eLog.Status_Start_Time__c.getTime();      
               }
               milliSeconds = otherHours - newHours;
               seconds = milliSeconds / 1000;
               totalMinutes = seconds / 60;
               totalHours = totalMinutes / 60;
               operationalEventInstance.NewToCreatedTotalHours__c = totalHours;
               operationalEventList.add(operationalEventInstance);
            }

            if(eventLog.sm1e__Operational_Event_Status__c == 'Work Order In Progress'){
               totalMinutes = 0;
               totalHours = 0;
               startTime = operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Start_Time__c;
               operationalEventInstance = new sm1e__Operational_Event__c();
               operationalEventInstance.Id = eventLog.sm1e__Operational_Event__c;
               for(sm1e__Event_Log__c  eLog : operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Event_Logs__r){
                   if(eLog.sm1e__Operational_Event_Status__c == 'New' && eLog.Status_Start_Time__c != null)
                      newHours = eLog.Status_Start_Time__c.getTime();
                   if(eLog.sm1e__Operational_Event_Status__c == 'Work Order In Progress' && eLog.Status_Start_Time__c != null)
                      otherHours = eLog.Status_Start_Time__c.getTime();
               }
               milliSeconds = otherHours - newHours;
               seconds = milliSeconds / 1000;
               totalMinutes = seconds / 60;
               totalHours = totalMinutes / 60;
               operationalEventInstance.New_to_In_progress__c = totalHours;
               operationalEventList.add(operationalEventInstance);
            }

            if(eventLog.sm1e__Operational_Event_Status__c == 'Work Order Completed'){
               totalMinutes = 0;
               totalHours = 0;
               startTime = operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Start_Time__c;
               operationalEventInstance = new sm1e__Operational_Event__c();
               operationalEventInstance.Id = eventLog.sm1e__Operational_Event__c;
               for(sm1e__Event_Log__c  eLog : operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Event_Logs__r){
                   if(eLog.sm1e__Operational_Event_Status__c == 'New' && eLog.Status_Start_Time__c != null)
                      newHours = eLog.Status_Start_Time__c.getTime();
                   if(eLog.sm1e__Operational_Event_Status__c == 'Work Order Completed' && eLog.Status_Start_Time__c != null)
                      otherHours = eLog.Status_Start_Time__c.getTime();
               }
               milliSeconds = otherHours - newHours;
               seconds = milliSeconds / 1000;
               totalMinutes = seconds / 60;
               totalHours = totalMinutes / 60;
               operationalEventInstance.NewToCompletedHotalHours__c = totalHours;

               operationalEventList.add(operationalEventInstance);
            }
        }     
    }

    if(operationalEventList != null && !operationalEventList.isEmpty())
       update operationalEventList;
}

The way i wrote the if conditions are similar for all three use cases but unfortunately, the calculation between "New" to "Work order In progress" is not working.

Can anyone tell me what changes do I have to make so that the trigger works the desired way.

  • you should refactor your code putting common processing logic in a method – Santanu Boral Jan 16 '18 at 21:54
1

If you refactor your code like this way you could able to solve your issue. Though, there are potentials to optimize it more, at-least it is a good start.

trigger EventLogTrigger on sm1e__Event_Log__c (after insert, after update) {
    Set<String> operationalEventIdSet = new Set<String>();    
    List<sm1e__Operational_Event__c> operationalEventList = new List<sm1e__Operational_Event__c>();

    for(sm1e__Event_Log__c eventLog : Trigger.new){
       operationalEventIdSet.add(eventLog.sm1e__Operational_Event__c);     
    }

    Map<Id, sm1e__Operational_Event__c> operationalEventMap = new Map<Id, sm1e__Operational_Event__c>([SELECT Id, 
                                                                          sm1e__Status__c, 
                                                                          sm1e__Start_Time__c, 
                                                                          (SELECT Id, 
                                                                                  Status_Start_Time__c, 
                                                                                  sm1e__Operational_Event_Status__c 
                                                                           FROM sm1e__Event_Logs__r 
                                                                           ORDER BY CreatedDate ASC) 
                                                                   FROM sm1e__Operational_Event__c WHERE Id IN: operationalEventIdSet]);

    for(sm1e__Event_Log__c eventLog : Trigger.new){
        if(operationalEventMap.keySet().size()>0 && operationalEventMap.containsKey(eventLog.sm1e__Operational_Event__c))
        {
            sm1e__Operational_Event__c operationalEventInstance = new sm1e__Operational_Event__c();
            operationalEventInstance.Id = eventLog.sm1e__Operational_Event__c;
            if(eventLog.sm1e__Operational_Event_Status__c == 'Work Order Created')
            {
                operationalEventInstance.NewToCreatedTotalHours__c = EventLogTriggerHandler.processLogic(operationalEventMap,eventLog, eventLog.sm1e__Operational_Event_Status__c);             
            }
            else if(eventLog.sm1e__Operational_Event_Status__c == 'Work Order In Progress')
            {
               operationalEventInstance.New_to_In_progress__c = EventLogTriggerHandler.processLogic(operationalEventMap,eventLog, eventLog.sm1e__Operational_Event_Status__c);               
            }           
            else if(eventLog.sm1e__Operational_Event_Status__c == 'Work Order Completed'){               
               operationalEventInstance.NewToCompletedHotalHours__c = EventLogTriggerHandler.processLogic(operationalEventMap,eventLog, eventLog.sm1e__Operational_Event_Status__c);               
            }
            operationalEventList.add(operationalEventInstance);
        }     
    }    
    update operationalEventList;
}

Common processing logic

public class EventLogTriggerHandler
{
    Long newHours;
    Long otherHours; 
    Long milliSeconds;
    Double totalMinutes = 0, totalHours = 0;
    Long seconds;
    DateTime startTime;

    public static Double processLogic(Map<Id, sm1e__Operational_Event__c> operationalEventMap,
        sm1e__Event_Log__c eventLog, String strStatus)
    {
        totalMinutes = 0;
        totalHours = 0;
        startTime = operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Start_Time__c;

        for(sm1e__Event_Log__c  eLog : operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Event_Logs__r)
        {
           if(eLog.sm1e__Operational_Event_Status__c == 'New' && eLog.Status_Start_Time__c != null)
              newHours = eLog.Status_Start_Time__c.getTime();
           if(eLog.sm1e__Operational_Event_Status__c == strStatus && eLog.Status_Start_Time__c != null)
              otherHours = eLog.Status_Start_Time__c.getTime();      
        }
        milliSeconds = otherHours - newHours;
        seconds = milliSeconds / 1000;
        totalMinutes = seconds / 60;
        totalHours = totalMinutes / 60;

        return totalHours;
    }
}

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.