-1

I am working on the BusinessHours and I am getting the below error. I just want to calculate the no. of hours difference between MondayStartTime and MondayEndTime

Method does not exist or incorrect signature: [BusinessHours].diff(Id, Time, Time)

The code which I used

BusinessHours bh = [SELECT FridayEndTime,FridayStartTime,Id,IsActive,IsDefault,LastModifiedById,
                              LastModifiedDate,MondayEndTime,MondayStartTime,Name,SaturdayEndTime,
                              SaturdayStartTime,SundayEndTime,SundayStartTime,SystemModstamp,ThursdayEndTime,
                              ThursdayStartTime,TimeZoneSidKey,TuesdayEndTime,TuesdayStartTime,
                              WednesdayEndTime,WednesdayStartTime FROM BusinessHours where Name='XXX Hours'
                               LIMIT 1];
        System.debug('bh.MondayStartTime :: '+bh.MondayStartTime);
        System.debug('bh.MondayEndTime  :: '+bh.MondayEndTime);
        System.debug('-------------------------------------------------------------');
        Datetime MondayStartDT = Datetime.newInstance(Date.Today(), bh.MondayStartTime);
        System.debug('MondayStartDT :: '+MondayStartDT);
        Datetime MondayEndDT = Datetime.newInstance(Date.Today(), bh.MondayEndTime);
        System.debug('MondayEndDT :: '+MondayEndDT);
        Long diff = BusinessHours.diff(bh.Id, MondayStartDT, MondayEndDT);
        System.debug('diff  :: '+diff);

Edit: As per thatherahere code I am getting following output I dont see its calculating the diff correctly.

enter image description here

Now Updated

0
0

diff(businessHoursId, startDate, endDate) is a static method and static methods belongs to the class, not to the object.

Signature

public static Long diff(String businessHoursId, Datetime startDate, Datetime endDate)

You must call it with BusinessHours class like:

BusinessHours bh = [SELECT FridayEndTime,FridayStartTime,Id,IsActive,IsDefault,LastModifiedById,
                                  LastModifiedDate,MondayEndTime,MondayStartTime,Name,SaturdayEndTime,
                                  SaturdayStartTime,SundayEndTime,SundayStartTime,SystemModstamp,ThursdayEndTime,
                                  ThursdayStartTime,TimeZoneSidKey,TuesdayEndTime,TuesdayStartTime,
                                  WednesdayEndTime,WednesdayStartTime FROM BusinessHours where Name='XXX Hours'
                                   LIMIT 1];

Datetime MondayStartDT = Datetime.newInstance(Date.Today(), bh.MondayStartTime);
Datetime MondayEndDT = Datetime.newInstance(Date.Today(), bh.MondayEndTime);
Long diff = BusinessHours.diff(bh.Id, bh.MondayStartDT, bh.MondayEndDT);

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

I hope this helps.

0
0
Datetime dt = Bh.diff(bh.Id, bh.MondayStartTime, bh.MondayEndTime);

This method accept parameters of type dateTime diff(businessHoursId, startDate, endDate) where you are passing Time parameter. You need to convert them into dateTime (Which I don't think easily possible in your use case.).

Or you need to parse it into minute Hours and then need to subtract.

to parse it you can use this method and simply call it with

differ(bh.Id, bh.MondayStartTime, bh.MondayEndTime);

function long differ(Id recordId, time dailyStartTime, time dailyEndTime) {
    Datetime StartDT = Datetime.newInstance(Date.Today(), dailyStartTime);
    Datetime EndDT = Datetime.newInstance(Date.Today(), dailyEndTime);
   return BusinessHours.diff(recordId, StartDT , bh.EndDT );
}

Time Class

BusinessHours Class

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.