Check if date/time before 8am next business day

kristenankeny
Tera Guru

I'm working on our change module and looking to script a risk condition to check to see if the planned start is before 8am eastern on the next business day. Would someone be able to provide me the best way to accomplish this? I assume I'd want to use a schedule that captures our normal business hours and holidays, which we have in place. But, I'm not positive where to start to set a GlideDateTime to next business day at 8 am so that I can compare it to the selected planned start.

1 ACCEPTED SOLUTION

ccajohnson
Kilo Sage

I was not sure if there is an existing OOB function to get the start of the next business day based on a schedule, so I wrote one. You can run this in Scripts background to see what is happening:



var schedID = '08fcd0830a0a0b2600079f56b1adb9ae'; //8-5 weekdays


var nBusDay = getStartNextBusDay(schedID);


gs.print(nBusDay);



function getStartNextBusDay(schedID) {


//       getStartNextBusDay: function(schedID) {


              schedRec = new GlideRecord('cmn_schedule_span');


              schedRec.addQuery('schedule', schedID);


              schedRec.query();


              var schedStartTime = '';


              if (schedRec.next()) {


                      var schedSDT = schedRec.start_date_time.getDisplayValue();


                      var SdtArray = schedSDT.split(' ');


                      schedStartTime = SdtArray [1];


              }


              var nowDate = new GlideDate();


              nowDate.addDays(1);


              var busStartDT = new GlideDateTime(nowDate.toString() + ' ' + schedStartTime);


              return busStartDT;


//       },


}



Currently the function is written to run from a Business Rule. If you want to place this function into a script include, the lines that are commented out can be used instead. Most likely you will be checking a date/time value from your form against the Start of the next business day. This should at least get you started and have a date/time to check against.


View solution in original post

6 REPLIES 6

ccajohnson
Kilo Sage

I was not sure if there is an existing OOB function to get the start of the next business day based on a schedule, so I wrote one. You can run this in Scripts background to see what is happening:



var schedID = '08fcd0830a0a0b2600079f56b1adb9ae'; //8-5 weekdays


var nBusDay = getStartNextBusDay(schedID);


gs.print(nBusDay);



function getStartNextBusDay(schedID) {


//       getStartNextBusDay: function(schedID) {


              schedRec = new GlideRecord('cmn_schedule_span');


              schedRec.addQuery('schedule', schedID);


              schedRec.query();


              var schedStartTime = '';


              if (schedRec.next()) {


                      var schedSDT = schedRec.start_date_time.getDisplayValue();


                      var SdtArray = schedSDT.split(' ');


                      schedStartTime = SdtArray [1];


              }


              var nowDate = new GlideDate();


              nowDate.addDays(1);


              var busStartDT = new GlideDateTime(nowDate.toString() + ' ' + schedStartTime);


              return busStartDT;


//       },


}



Currently the function is written to run from a Business Rule. If you want to place this function into a script include, the lines that are commented out can be used instead. Most likely you will be checking a date/time value from your form against the Start of the next business day. This should at least get you started and have a date/time to check against.


Hi Christopher,



So my requirement is to check two things: that I'm submitting the request before the end of the business day and that my start is after 8am the next day. This script is working for me, right now, but I'm wondering if you have an idea of whether I need to make adjustments for situations like people in other time zones. We have some users who work offshore and the business hours are determined by a team that works in US Eastern.



The results of the script:


*** Script: 2017-11-13 08:00:00


*** Script: 2017-11-10 17:00:00


*** Script: OK: Submitted before 5pm


*** Script: BAD: Planned start before 8am next business day



Script:


var schedID = '090eecae0a0a0b260077e1dfa71da828'; //8-5 weekdays


var nBusDay = getStartNextBusDay(schedID);


gs.print(nBusDay);


var thisBusDay = getEndThisDay(schedID);


gs.print(thisBusDay);




var ch = new GlideRecord('change_request');


ch.addQuery('sys_id','c83c5e5347c12200e0ef563dbb9a7190'); //this is a change request with a planned start some time tonight, so off business hours


ch.query();


if(ch.next()){


var rightNow = new GlideDateTime();


if(rightNow.getDisplayValue() < thisBusDay){


gs.print('OK: Submitted before 5pm');


}


else{


gs.print('BAD: Submitted after 5pm');


}


if(ch.start_date < nBusDay){


gs.print('BAD: Planned start before 8am next business day');


}


else{


gs.print('OK: Planned start after 8am next business day');


}


}



function getStartNextBusDay(schedID) {


//       getStartNextBusDay: function(schedID) {


              schedRec = new GlideRecord('cmn_schedule_span');


              schedRec.addQuery('schedule', schedID);


              schedRec.query();


              var schedStartTime = '';


              if (schedRec.next()) {


                      var schedSDT = schedRec.start_date_time.getDisplayValue();


                      var SdtArray = schedSDT.split(' ');


                      schedStartTime = SdtArray [1];


              }


              var nowDate = new GlideDate();


              nowDate.addDays(1);


var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');


var whenNext = schedule.whenNext(nowDate);


nowDate.add(whenNext);


              var busStartDT = new GlideDateTime(nowDate.toString() + ' ' + schedStartTime);


              return busStartDT;


//       },


}




function getEndThisDay(schedID) {


//       getStartNextBusDay: function(schedID) {


              schedRec = new GlideRecord('cmn_schedule_span');


              schedRec.addQuery('schedule', schedID);


              schedRec.query();


              var schedStartTime = '';


              if (schedRec.next()) {


                      var schedSDT = schedRec.end_date_time.getDisplayValue();


                      var SdtArray = schedSDT.split(' ');


                      schedEndTime = SdtArray [1];


              }


              var nowDate = new GlideDate();


            // nowDate.addDays(1);


//var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');


//var whenNext = schedule.whenNext(nowDate);


//nowDate.add(whenNext);


              var busEndDT = new GlideDateTime(nowDate.toString() + ' ' + schedEndTime);


              return busEndDT;


//       },


}


kristenankeny
Tera Guru

Thank you so much! I just had to add in a bit to get when Next to be sure that it skips the weekends and it returned exactly the date/time I expected.


Just Realized that I forgot about getting the next business day based upon the schedule, so I added it into the script:



var schedID = '08fcd0830a0a0b2600079f56b1adb9ae'; //8-5 weekdays


var nBusDay = getStartNextBusDay(schedID);


gs.print(nBusDay);



function getStartNextBusDay(schedID) {


//       getStartNextBusDay: function(schedID) {


              schedRec = new GlideRecord('cmn_schedule_span');


              schedRec.addQuery('schedule', schedID);


              schedRec.query();


              var schedStartTime = '';


              if (schedRec.next()) {


                      var schedSDT = schedRec.start_date_time.getDisplayValue();


                      var SdtArray = schedSDT.split(' ');


                      schedStartTime = SdtArray [1];


              }


              var nowDate = new GlideDate();


              var sdt = new GlideDateTime(nowDate.toString() + ' ' + schedStartTime);


              var dc = new DurationCalculator();


              dc.setSchedule(schedID, 'US/Eastern');


              var cDuration = 32400; //9 hours


              if (!dc.calcDuration(cDuration)) {


                      gs.log("*** Error calculating duration");


                      return;


              }


              var edt = new GlideDateTime(dc.getEndDateTime().getDate().toString() + ' ' + schedStartTime)


              return edt;


//       },


}