1

I have a question in BusinessHour NextstartDate method. When I am using nextStartDate(businessHoursId, targetDate) method , I am not getting the Start time of next business day. Instead it is defaulted to 9.00AM irrespective of time zone. Any thoughts to fetch the start time of next start business day.

// Starting from the targetTime, find the next date when business hours reopens.(This is what is given in the website)

I have am having businesshours setup from 11.AM - 5.PM EST( Monday - Friday and saturday,sunday is holiday).

When I am trying to execute my code in workbench

String StrD = '2016-06-03 13:00:00';
Datetime StartDate = Datetime.valueOf(StrD);
system.debug('*StartDate*'+StartDate);
String StrDEnd = '2016-06-04 18:00:00'; // Saturday which is a holiday 
Datetime EnddDate = Datetime.valueOf(StrDEnd);
system.debug('*EnddDate*'+EnddDate.format());

list<BusinessHours> businehour = [SELECT FridayEndTime,FridayStartTime,Id,IsActive,IsDefault,MondayEndTime,MondayStartTime,Name,
SaturdayEndTime,SaturdayStartTime,SundayEndTime,SundayStartTime,SystemModstamp,ThursdayEndTime,ThursdayStartTime,
TimeZoneSidKey,TuesdayEndTime,TuesdayStartTime,WednesdayEndTime,WednesdayStartTime from BusinessHours where Name ='11To5EST'];

Datetime nextStart = BusinessHours.nextStartDate(businehour[0].id, EnddDate);

system.debug('*nextStart*'+nextStart.format());

Output: *nextStart*6/6/2016 9:00 AM***

o/P:13:00:40.11 (11295030)|EXECUTION_STARTED 13:00:40.11
(11310427)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
13:00:40.11 (12093809)|USER_DEBUG|[3]|DEBUG|*StartDate*2016-06-03
18:00:00 13:00:40.11
(12259171)|USER_DEBUG|[6]|DEBUG|*EnddDate*6/4/2016 6:00 PM
***13:00:40.11 (18316761)|USER_DEBUG|[14]|DEBUG|*nextStart*6/6/2016 9:00 AM***13:00:40.11
(18357887)|CODE_UNIT_FINISHED|execute_anonymous_apex 13:00:40.11
(19679036)|EXECUTION_FINISHED

1

As per Salesforce documentation. The returned time by BusinessHours.nextStartDate is in the local time zone (GMT).

The default .format() method for Datetime converts the Date and time into the User's time zone.

If you want to get the same Date and Time as in your business hour, then you have to format in that timezone. Like in your case if business hour timezone is (GMT-04:00) Eastern Daylight Time (America/New_York) then you can format the date as below. This will solve your problem.

System.debug('nextStart = '+nextStart.format('MM/dd/yyyy HH:mm:ss','America/New_York')); 
  • :I want to store that value in a variable for further use in my code. When I tried to save that formatted value Im unable to. I have tried these 2 frmats,Datetime nextStart = BusinessHours.nextStartDate(businehour[0].id, EnddDate); String StrnextStart = nextStart.format('MM/dd/yyyy HH:mm:ss','America/Chicago'); (1)nextStart = datetime.parse(StrnextStart ); (2)nextStart = datetime.valueOf(StrnextStart ); ERROR: EXCEPTION: System.TypeException: Invalid date/time: 06/06/2016 10:00:00 Can I save the exact same value with local time in a variable? :*nextStart*06/06/2016 10:00:00 – Shaan Jun 2 '16 at 15:22
  • The DateTime should not be created by parsing the date string again. Just format the nextStart date variable in the timezone required. For example if you want to use user's timezone, the code should be : String StrnextStart = nextStart.format('MM/dd/yyyy HH:mm:ss'); – Saroj Bera Jun 3 '16 at 6:02
0

Took me some time to get it.

nextStartDate method output respects TimeZone of BuisnessHours record AND it respects timezone of running user.

Most likely user you are running under has 2 hours difference to EST (and even more likely it has PDT time) therefore you see 11 AM as 9 AM, as 11 AM EST is 9 AM PDT.

  • I am in CST timezone which should be 10AM CST. I am bit confused if I can save the formatted date in a variable instead of in UTC timezone? – Shaan Jun 2 '16 at 19:06
  • you should save everything in GMT time, as Salesforce does so, especially if value will be shown by UI. UI will convert time and show logged user correct time according to his timezone. – Andrei.Z Jun 3 '16 at 0:48
-1
// Get the default business hours
BusinessHours busshrs = [SELECT Id FROM BusinessHours WHERE IsDefault = true];

// If it is within the business hours. The returned time will be in the local time zone
Datetime nextStart = BusinessHours.nextStartDate(busshrs.id, System.today());

System.debug('Next Business Hours Start Date is => ' + nextStart);

Let say, Business hours for your Salesforce Org are 9 AM to 6 PM. So there are 2 results based on below:

  1. If you execute the above code within the business hours, then it will return today's date.

  2. If you execute the above code outside the business hours, then it will return next open business date.

Check What is the use of nextStartDate in Salesforce more information.

  • 1
    Welcome to SFSE. Please be careful not to link to too many of your own blog posts, or you may be viewed as overly self promotional. Once in a while is fine, just something to keep in mind. – Adrian Larson Jul 29 '18 at 19:29

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.