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
sfadm sfadmsfadm sfadm 

Need to calculate the out of office hours in apex

I need to calculate the out of office hours in apex.

The hours are:
6 pm – 9 am Mon – Fri
and
6 pm on Fri till 9 am on Mon

I've already configured the Business Hours:

Monday09:00to18:00
Tuesday09:00to18:00
Wednesday09:00to18:00
Thursday09:00to18:00
Friday09:00to18:00
Saturday24 Hours
Sunday24 Hours

and activate it in Salesforce.

Please advise how it can be achieved in apex?

Best Answer chosen by sfadm sfadm
JSingh9JSingh9
Hi,

For any Day first thing you can check is If the date is a Business Day of not using below method
BusinessHours.isWithin(businessHoursId, targetDate)
businessHoursId will be the calendar Id
targetDate will be the date for which u are looking to calculate business hours

Example
// Get the default business hours
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Create Datetime on May 28, 2013 at 1:06:08 AM in the local timezone.
Datetime targetTime = Datetime.newInstance(2013, 5, 28, 1, 6, 8);

// Find whether the time is within the default business hours
Boolean isWithin= BusinessHours.isWithin(bh.id, targetTime);
 Using the same developer refrence,

If it returns true, u can take the tagretDate time and subtract it from 18:00 number of pending hours will give you business hours remaining for that day.

Does that help?

Thanks


 

All Answers

JSingh9JSingh9
You can use BusinessHours Class to do further calculations https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_businesshours.htm

Let  me know if any further help you need.

Thanks,
JS
sfadm sfadmsfadm sfadm
Hi Jaspreet,

I need to get the working hours in the same bussines day in the following code:
 
while(workingHours > 0) {
            // convert workingHours to seconds
            Long secondsWorkingHours = (Long) (workingHours * 60 * 60);
            System.debug('secondsWorkingHours ' + secondsWorkingHours);
            // difference between current time and endTime
            Long diff = endWorkingTime.getTime() - now.getTime();
            System.debug('diff ' + diff);
            // check if workingHours fit in the business day
            if (secondsWorkingHours < diff) { 
                // if (true)
                // workingHours is withing the same business day
                
            } else {
                // workingHours is outside of the same business day
                
            }
}

In the while loop of the ELSE clause I'm subtracting the workingHours from diff but I'm stuck in the IF clause. In the IF clause I need to get the working hours in the same bussines day.

I need some clue how to achieve that.
JSingh9JSingh9
Hi,

For any Day first thing you can check is If the date is a Business Day of not using below method
BusinessHours.isWithin(businessHoursId, targetDate)
businessHoursId will be the calendar Id
targetDate will be the date for which u are looking to calculate business hours

Example
// Get the default business hours
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Create Datetime on May 28, 2013 at 1:06:08 AM in the local timezone.
Datetime targetTime = Datetime.newInstance(2013, 5, 28, 1, 6, 8);

// Find whether the time is within the default business hours
Boolean isWithin= BusinessHours.isWithin(bh.id, targetTime);
 Using the same developer refrence,

If it returns true, u can take the tagretDate time and subtract it from 18:00 number of pending hours will give you business hours remaining for that day.

Does that help?

Thanks


 
This was selected as the best answer