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