Guest User

Untitled

a guest
Jan 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. Long newHours;
  2. Long otherHours;
  3. Long milliSeconds;
  4. Double totalMinutes = 0, totalHours = 0;
  5. Long seconds;
  6. DateTime startTime;
  7. Set<String> operationalEventIdSet = new Set<String>();
  8. sm1e__Operational_Event__c operationalEventInstance = null;
  9. List<sm1e__Operational_Event__c> operationalEventList = new List<sm1e__Operational_Event__c>();
  10.  
  11. for(sm1e__Event_Log__c eventLog : Trigger.new){
  12. operationalEventIdSet.add(eventLog.sm1e__Operational_Event__c);
  13. }
  14.  
  15. Map<Id, sm1e__Operational_Event__c> operationalEventMap = new Map<Id, sm1e__Operational_Event__c>();
  16. operationalEventMap = new Map<Id, sm1e__Operational_Event__c>([SELECT Id,
  17. sm1e__Status__c,
  18. sm1e__Start_Time__c,
  19. (SELECT Id,
  20. Status_Start_Time__c,
  21. sm1e__Operational_Event_Status__c
  22. FROM sm1e__Event_Logs__r
  23. ORDER BY CreatedDate ASC)
  24. FROM sm1e__Operational_Event__c WHERE Id IN: operationalEventIdSet]);
  25.  
  26. for(sm1e__Event_Log__c eventLog : Trigger.new){
  27. if(operationalEventMap != null && operationalEventMap.containsKey(eventLog.sm1e__Operational_Event__c)){
  28. if(eventLog.sm1e__Operational_Event_Status__c == 'Work Order Created'){
  29. totalMinutes = 0;
  30. totalHours = 0;
  31. startTime = operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Start_Time__c;
  32. operationalEventInstance = new sm1e__Operational_Event__c();
  33. operationalEventInstance.Id = eventLog.sm1e__Operational_Event__c;
  34. for(sm1e__Event_Log__c eLog : operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Event_Logs__r){
  35. if(eLog.sm1e__Operational_Event_Status__c == 'New' && eLog.Status_Start_Time__c != null)
  36. newHours = eLog.Status_Start_Time__c.getTime();
  37. if(eLog.sm1e__Operational_Event_Status__c == 'Work Order Created' && eLog.Status_Start_Time__c != null)
  38. otherHours = eLog.Status_Start_Time__c.getTime();
  39. }
  40. milliSeconds = otherHours - newHours;
  41. seconds = milliSeconds / 1000;
  42. totalMinutes = seconds / 60;
  43. totalHours = totalMinutes / 60;
  44. operationalEventInstance.NewToCreatedTotalHours__c = totalHours;
  45. operationalEventList.add(operationalEventInstance);
  46. }
  47.  
  48. if(eventLog.sm1e__Operational_Event_Status__c == 'Work Order In Progress'){
  49. totalMinutes = 0;
  50. totalHours = 0;
  51. startTime = operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Start_Time__c;
  52. operationalEventInstance = new sm1e__Operational_Event__c();
  53. operationalEventInstance.Id = eventLog.sm1e__Operational_Event__c;
  54. for(sm1e__Event_Log__c eLog : operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Event_Logs__r){
  55. if(eLog.sm1e__Operational_Event_Status__c == 'New' && eLog.Status_Start_Time__c != null)
  56. newHours = eLog.Status_Start_Time__c.getTime();
  57. if(eLog.sm1e__Operational_Event_Status__c == 'Work Order In Progress' && eLog.Status_Start_Time__c != null)
  58. otherHours = eLog.Status_Start_Time__c.getTime();
  59. }
  60. milliSeconds = otherHours - newHours;
  61. seconds = milliSeconds / 1000;
  62. totalMinutes = seconds / 60;
  63. totalHours = totalMinutes / 60;
  64. operationalEventInstance.New_to_In_progress__c = totalHours;
  65. operationalEventList.add(operationalEventInstance);
  66. }
  67.  
  68. if(eventLog.sm1e__Operational_Event_Status__c == 'Work Order Completed'){
  69. totalMinutes = 0;
  70. totalHours = 0;
  71. startTime = operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Start_Time__c;
  72. operationalEventInstance = new sm1e__Operational_Event__c();
  73. operationalEventInstance.Id = eventLog.sm1e__Operational_Event__c;
  74. for(sm1e__Event_Log__c eLog : operationalEventMap.get(eventLog.sm1e__Operational_Event__c).sm1e__Event_Logs__r){
  75. if(eLog.sm1e__Operational_Event_Status__c == 'New' && eLog.Status_Start_Time__c != null)
  76. newHours = eLog.Status_Start_Time__c.getTime();
  77. if(eLog.sm1e__Operational_Event_Status__c == 'Work Order Completed' && eLog.Status_Start_Time__c != null)
  78. otherHours = eLog.Status_Start_Time__c.getTime();
  79. }
  80. milliSeconds = otherHours - newHours;
  81. seconds = milliSeconds / 1000;
  82. totalMinutes = seconds / 60;
  83. totalHours = totalMinutes / 60;
  84. operationalEventInstance.NewToCompletedHotalHours__c = totalHours;
  85.  
  86. operationalEventList.add(operationalEventInstance);
  87. }
  88. }
  89. }
  90.  
  91. if(operationalEventList != null && !operationalEventList.isEmpty())
  92. update operationalEventList;
Add Comment
Please, Sign In to add comment