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
aam1raam1r 

Get Business Hours Start of Day

Hi, i'm trying to record the start of day date/time value for a given Business Hour as part of an Apex trigger but not sure of the way to do this.  Can someone help me try and achieve this please? The basic logc is to check what day it is today and then record the Start of Day date/time in to a field  (Start_of_Day__c) for that same day defined for the Business Hour.

Thanks
Best Answer chosen by aam1r
AnjithKumarAnjithKumar
Dear ,

To achieve this functionality you have to set up business hours in your org. After set up business hours user following code in your trigger.
// Get the default business hours
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Starting from the System.now, find the next date when business hours reopens.
//  This returned next day start time will be in the local time zone

Datetime nextStart = BusinessHours.nextStartDate(bh.id,  System.now());

obj.Start_of_Day__c = nextStart.addDays(-1);

Hope it helps you. Let me know if it helps you.

Thanks,
Anjith Kumar.

All Answers

Naval Sharma4Naval Sharma4
Hi,

In order to get they day in your trigger use follow code snippet.
 
String dayName = '';
dayName = DateTime.now().format('EEE');  // This will return Tue, Wed etc.

if(dayName == 'Mon')
   Start_of_Day__c = yourDateTime
else if(dayName == 'Tue')
   .
   .
   .
   .
else if(dayName == 'Sun')   
   Start_of_Day__c = yourDateTime

I hope this will work for you.

Thanks
aam1raam1r
Naval – that’s a great start for me. However it’s the yourDateTime bit I’m struggling with when working with the Business Hours setup in Salesforce. I want something like this: String dayName = DateTime.now().format('EEE'); // This will return Tue, Wed etc. BusinessHours bh = [select id from BusinessHours where isDefault = True]; if(dayName == 'Mon') Start_of_Day__c = bh.MondayStartOfDay; else if(dayName == 'Tue') . . . However, I’m just not getting bh bit right and do not get the MondayStartOfDay etc. showing up as per https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_businesshours.htm
aam1raam1r
Oh, and thanks for getting back to me so promptly Naval!
Naval Sharma4Naval Sharma4
Try this.
 
BusinessHours bHours = [SELECT Id, MondayStartTime, TuesdayStartTime, SundayStartTime FROM BusinessHours WHERE IsActive = true LIMIT 1];  // You can also specify Id of this record in this query

String dayName = '';
dayName = DateTime.now().format('EEE');  // This will return Tue, Wed etc.

if(dayName == 'Mon')
   Start_of_Day__c = bHours.MondayStartTime;
   
else if(dayName == 'Tue')
	Start_of_Day__c = bHours.TuesdayStartTime;
   .
   .
   .
else if(dayName == 'Sun')   
   Start_of_Day__c = bHours.SundayStartTime;

 
AnjithKumarAnjithKumar
Dear ,

To achieve this functionality you have to set up business hours in your org. After set up business hours user following code in your trigger.
// Get the default business hours
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Starting from the System.now, find the next date when business hours reopens.
//  This returned next day start time will be in the local time zone

Datetime nextStart = BusinessHours.nextStartDate(bh.id,  System.now());

obj.Start_of_Day__c = nextStart.addDays(-1);

Hope it helps you. Let me know if it helps you.

Thanks,
Anjith Kumar.
This was selected as the best answer
Naval Sharma4Naval Sharma4
Have you already set your business hours?
aam1raam1r
Ok Guys,

Naval - firstly, love the last question.. or rather the tiiming of it!  Now, your solution gave me an error as i'm tryig to assign a time value to a date/time field.  I did not try to correct that before attemting to work at the remainig logic.

Anjith - thanks for your suggestion.  It worked as is.  and yes Business Hours were already defined.  it was teh xtraction of the date and time i was struggling with.

Thanks again guys.
aam1raam1r
Hi Anjith - any idea how i can extract the End Time for the day?
AnjithKumarAnjithKumar
Use below code
 
// Get the default business hours
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Starting from the System.now, find the next date when business hours reopens.
//  This returned next day start time will be in the local time zone

Datetime nextStart = BusinessHours.nextStartDate(bh.id,  System.now());

obj.Start_of_Day__c = nextStart.addDays(-1);

obj.End_of_Day__c = obj.Start_of_Day__c.addHours(BusinessHours.diff(bh.id,nextStart,nextStart.addDays(-1))/(3600000);

 
aam1raam1r
Anjith, I’m getting a message does not exist of incorrect signature error message for the following line: obj.End_of_Day__c = obj.Start_of_Day__c.addHours(BusinessHours.diff(bh.id,nextStart,nextStart.addDays(-1))/(3600000);
AnjithKumarAnjithKumar
there is  ')' missed in line 11 , below is updated code
 
// Get the default business hours
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Starting from the System.now, find the next date when business hours reopens.
//  This returned next day start time will be in the local time zone

Datetime nextStart = BusinessHours.nextStartDate(bh.id,  System.now());

obj.Start_of_Day__c = nextStart.addDays(-1);

obj.End_of_Day__c = obj.Start_of_Day__c.addHours(BusinessHours.diff(bh.id,nextStart,nextStart.addDays(-1))/(3600000));

 
aam1raam1r
Hi Anjith, Thanks again for replying. I had noticed the missing ‘)’ but it is still giving me: “Method does not exist or incorrect signature: [BusinessHours].diff(Id, Datetime, Datetime)”
AnjithKumarAnjithKumar
I have tested above code in my org then only posted here. could you please check following link for BusinessHours class

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

 
aam1raam1r
Yeah - i've looked at that also but can't seem to figure out why i'm still getting the error.  Here's my code snippet, which is essentially the same as what you've suggested:
 
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

DateTime nextStartDay = BusinessHours.nextStartDate(bh.id,  System.now());
obj.Start_of_Day__c = nextStart.addDays(-1);

DateTime nextEndDay = nextStartDay.addHours(bh.diff(bh.diff(bh.id,nextStart,nextStart.addDays(-1))/(3600000));
obj.End_of_Day__c = nextEndDay;

and its the second last line giving me 'Method does not exist or incorrect signature: [Datetime].addHours(Long)'

Note: i had to change second line from 'BusinessHours.id' to 'bh.id' as this also was throwing an error.  not sure how it worked for you though.
aam1raam1r
Anjith - please excuse my double diff (above) thats most definately not part of my current code.  However, any ideas where i could be going wrong with this?  Just to verify the corrected line of code that is throwing the mentioned error is:
DateTime nextEndDay = nextStartDay.addHours(BusinessHours.diff(bh.id, nextStartDay,nextStartDay.addDays(-1))/(3600000));

 
AnjithKumarAnjithKumar
Aam1r, can you please above line with following
 
​Long diff = BusinessHours.diff(bh.id, nextStartDay,nextStartDay.addDays(-1));
Integer  hours =(Integer) diff/3600000;
DateTime nextEndDay = nextStartDay.addHours(hours);

let me know if it works.


thanks,
Anjith
aam1raam1r
that has worked.  many thank Anjith for all your help.