Skip to main content

Trouble in using Business hours methods to compare business hours with specific number of hours in apex trigger

I'm trying to compare number of business hours with number of hours (bh<48) I have gone through few methods and tried the below method to get number of hours(difference between startdate and enddate :

diff(BusinessHours.id, startdate, Datetime.now())<48)

But I'm constantly getting the same error for all the methods : "Method does not exist or incorrect signature: [BusinessHours].diff(Schema.SObjectField, Datetime, Datetime)"

I dont understand this error as usage of methods are referred here

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_businesshours.htm

Any suggestions are highly appreciated! TIA


//Trigger to compare business hours with specific number of hours in apex trigger and update the case status value
    Trigger Test_Business_hours_Trigger on emailmessage (before insert,after insert,before update,after update) {
       // case cas {get;set;}
       long l;

    Case cas = [SELECT ClosedDate, status FROM Case WHERE ClosedDate != NULL LIMIT 1];
//SOQL query to get the business hours defined in the org 
       BusinessHours bh =
           [Select
             id,SundayStartTime, MondayStartTime, TuesdayStartTime,
             WednesdayStartTime, ThursdayStartTime, FridayStartTime,
             SaturdayStartTime, SundayEndTime, MondayEndTime,TuesdayEndTime,
             WednesdayEndTime, ThursdayEndTime, FridayEndTime,SaturdayEndTime
           From BusinessHours 
          Where IsDefault=true];
//gets closed date and system.now() in to the variables to define in business hours bh.diff() method
        DateTime startdate = cas.ClosedDate ;
        DateTime systemnow = system.now();
    
        long startl = startdate.getTime();
        long nowl   = systemnow.getTime();
// assigning the number of hours(business hours) i.e, the difference between the startdate and systemnow in to variable 'l'
        l = businesshours.diff(bh.id,startdate,systemnow);
        system.debug(l);
//For trigger.new values if status is closed and number of business hours are less than 48 hours(172800000 milliseconds) then updates the status to working
For(Emailmessage em : trigger.new){
        if((Cas.Status=='Closed') && (l<172800000)) {
            cas.status='Working';
            update Cas;
        }
}
    }