3

I am working on a requirement to update a field if the LastModifiedDate lies within the Business hours of the user, viz. Monday to Friday between 8 AM to 5 PM.

I have tried using the BusinessHours class alongwith isWithin method, but the calculation is based upon the timezone defined in the BusinessHours.

For example, the BusinessHours are defined in EST TimeZone, but user is working in IST timezone.

Do I need to create business hours records for all the applicable timezone(worldwide application support).

Is there any other simpler way to implement this, so that the constant working hours can be applied to all the user timezones?

Any help would be appreciated.

3

The easiest solution, if you are able to use Apex for this checking, is to first convert the user's date/time value to the business hours timezone then apply the check. Perhaps something like (you should add defensive handling of not finding the business hours etc. as needed):

Datetime toBeChecked = ...; // Whatever value, such as the LastModifiedDate from some object
Id bizHoursId = ...; // The ID for the business hours record you want to use

// Find the time zone for the business hours
BusinessHours hours = [SELECT TimeZoneSidKey FROM BusinessHours WHERE Id = :bizHoursId];
String bizTimeZoneSid = hours.TimeZoneSidKey;

// Now determine the offset between this and the user's timezone
TimeZone bizTimeZone = TimeZone.getTimeZone(bizTimeZoneSid);
TimeZone userTimeZone = UserInfo.getTimeZone();

// This is in seconds since the offsets are in milliseconds
Integer offset = (userTimeZone.getOffset(toBeChecked) - bizTimeZone.getOffset(toBeChecked)) / 1000;

// Determine the user's time in the business hours time zone
Datetime toBeCheckedInBizTimeZone = toBeChecked.addSeconds(offset);

// Finally do the check
if (BusinessHours.isWithin(bizHoursId, toBeCheckedInBizTimeZone)) {
   ...
}

Note that this doesn't consider business hours that include different holidays, but then that would simply require you to select the correct bizHoursId. A small optimization could be added to avoid the generation of an offset when both time zones are the same.

6
  • Thanks for the answer. But unfortunately, by adding the offset we would change the time, correct? For example 14:40(IST) hrs gets converted to 04:10 Hrs(EST) and then 4.10 am is not in the business hours, but 14:40 was valid in the business hour(i.e. it lies between 8 am to 5 pm). But the output says it is not within the business hours – Hemant Jain Jun 9 '20 at 11:34
  • OK, so I probably should have not put the - on the offset (I wrote the code "virtually"). Try changing the calculation to Datetime toBeCheckedInBizTimeZone = toBeChecked.addSeconds(offset); – Phil W Jun 9 '20 at 12:35
  • My Bad. Thanks. This works in most of the scenarios I have tested work with this. Now only thing, I am thinking is this won't incorporate holidays for checking business days, but if it is just Mon to Fri business hours, this works perfectly. Correct me if otherwise. – Hemant Jain Jun 9 '20 at 15:52
  • 1
    This won't adapt the holidays, for sure. To do that you need multiple Business Hours definitions. (By time zone may help, but even this doesn't necessarily give you what you need since UK holidays are different to Portuguese holidays whilst sharing the same time zone, so you may need to create per-country/state/region Business Hours instances.) – Phil W Jun 9 '20 at 16:25
  • btw, did changing the offset sign make it work how you wanted? If so I can update my answer. – Phil W Jun 9 '20 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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