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

kristenankeny I like your method better (shorter and more concise)


I got it from another community post. So, I needed this for a risk condition. Here is my final script, that takes into account differences in timezone (the time logic works so far based on testing - I need to wait until after 5 to test that the condition triggers properly).




if(current.type == 'normal' && current.state.changes() && current.state == -4){


var schedID = 'b9f03cea4f568f44bd3328928110c7f2'; //8-5 Eastern without company Holidays


var schedGR = getSchedule(schedID);


var nBusDay = getStartNextBusDay(schedGR);


var thisBusDay = getEndThisDay(schedGR);


var tz = Packages.java.util.TimeZone.getTimeZone("US/Eastern");


var rightNow = new GlideDateTime();


rightNow.setTZ(tz);


if(rightNow.getDisplayValue() > thisBusDay && current.start_date < nBusDay){


answer = true;


}


else{


answer = false;


}


}


else{


answer = false;


}



function getSchedule(schedID){


schedRec = new GlideRecord('cmn_schedule_span');


schedRec.addQuery('schedule',schedID);


schedRec.query();


if(schedRec.next()){


return schedRec;


}


}




function getStartNextBusDay(schedGR){


var schedStartTime = '';


var schedSDT = schedGR.start_date_time.getDisplayValue();


var SdtArray = schedSDT.split(' ');


schedStartTime = SdtArray[1];


var nowDate = new GlideDate();


nowDate.addDays(1);


var schedule = new GlideSchedule('b9f03cea4f568f44bd3328928110c7f2');


var whenNext = schedule.whenNext(nowDate);


nowDate.add(whenNext);


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


return busStartDT;


}




function getEndThisDay(schedGR){


var schedEndTime = '';


var schedEDT = schedGR.end_date_time.getDisplayValue();


var SetArray = schedEDT.split(' ');


schedEndTime = SetArray[1];


var nowDate = new GlideDate();


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


return busEndDT;


}