function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
arishi0arishi0 

Error using hte diff method of the BusinessHours objects

I have a trigger that needs to use a custom BusinessHours object I created that defines a the valid days and hours for a workgroup that uses the app. The problem I have is that I need to hold the BusinesHours object so that I can use the diff method to calculate the hours of difference between the last status change and the curent status change of another custom object(work order).

 

The code is as follows and the problem is in the "DS_BusinessHours = [SELECT...." line:

 

trigger DS_WO_Stop_Statuses_update on DS_Work_Order__c (after insert, after update) {

 

     

      //Declare vars

      List<Stop_Status__c> StatusList = New List<Stop_Status__c> ();

      BusinessHours DS_BusinessHours = null;

 

//If new Work Order then initialize fields at 0 or equivalent;

if (Trigger.isInsert){

      for(DS_Work_Order__c newWO:System.Trigger.new) {

            newWO.WO_Last_Status_Changed__c= System.now();

            newWO.WO_Time_with_DS__c=0;

     

      }}else{    

//2.get the previous status and it's time and add the

 For (DS_Work_Order__c newWO:System.Trigger.new){

      DS_Work_Order__c oldWO = System.Trigger.oldmap.get(newWO.Id);

       if (oldWo.Status__c!=newWO.Status__c && newWO.WO_Last_Status_Changed__c !=null) {

                  if(newWO.Status__c != 'Pending'){

                        DS_BusinessHours = [SELECT Id From BusinessHours WHERE Name = 'DS_BusinessHours'];

                        Double timeSinceLastStatus = BusinessHours.diff(DS_BusinessHours, newWO.WO_Last_Status_Changed__c, System.now())/3600000.0;

                        newWO.WO_Time_with_DS__c += timeSinceLastStatus;

                        }      

                  }

            }

  }//end else

}//end trigger

 

The problem I get is that im assigning an Id to a business object and then when trying to do the diff method to calculate the difference it throws an exception. My question….How can I get the business hours so I can calculate the diff method?

Please if you have any input I’d appreciate it greatly.