Salesforce: Adding specific business days to a date using BusinessHours class

Businesshours is a system class provided by Salesforce for performing various operations on the DateTime values. It provides methods for checking if a date falls during business hours, whats the diff between two dates in business.

The in-built method does allow adding the specific days to the DateTime value but there is a catch, it needs to be in the Long format. More information could be found here.

add(businessHoursId, startDate, intervalMilliseconds)

What exactly this method does?

The method has been specifically designed to add the number of hours in the millisecond format. And addition is done with respect to business hours. Let’s consider you want to add 1 day to the Date December 18th, 10 AM. And business hours are set for 8 AM to 8 PM for all days.

https://gist.github.com/prasannadeshpande/fc8df805bfdc3d82afc62b74c28db438

This calculation added complete 2 days as business hours are set 12 hours a day.

Alternative for avoiding such hour based calculation could be going in an iterative way.

https://gist.github.com/prasannadeshpande/607d606d3c95d4dab0adafde2235b999

This will also ensure that the initial time is preserved.


Posted

in

, , ,

by

Tags:

Comments

One response to “Salesforce: Adding specific business days to a date using BusinessHours class”

  1. […] UPDATE: If you want to consider holidays and business hours you can check out this article. […]

Leave a Reply

Your email address will not be published. Required fields are marked *