Add a schedule lookup for client script/script include

booher04
Tera Guru

I apologize for the long post.  I'm trying to add a schedule into my current script include and can't seem to get it working correctly.  I need to look at a date variable and if it's less than 7 business days, mark a yes/no field as 'yes'.   It works for 7 days but not for business days. Our requirement changed from calendar days to business days.  

Here's the base script include:

var ajaxDateDiff = Class.create();
ajaxDateDiff.prototype = Object.extendsObject(AbstractAjaxProcessor, {

check5Days: function () {

var valid = this.getParameter('sysparm_cdt');
var gst = new GlideDateTime(gs.now());
gst.addDays(7);

var diffSeconds = gs.dateDiff(gst, valid, true);

if (diffSeconds < 0 )
{
return 'true';
}
else
{
return 'false';
}
}
});

My schedule is '8-8 weekdays excluding weekends'.  I tried to add in the following script but I couldn't get it to work correctly.  Any advice?


addTimeSchedule: function() {

var start = this.getParameter('requested_delivery_date');

var timeToAdd = this.getParameter('time_to_add');

var sch = this.getParameter('schedule');

var gdt = new GlideDateTime();

gdt.setDisplayValue(start);

//Get a schedule by name to calculate duration

var schedRec = new GlideRecord('cmn_schedule');

schedRec.get('8-8 weekdays excluding holidays', sch);

if (typeof GlideSchedule != 'undefined')

var sched = new GlideSchedule(schedRec.sys_id);

else

var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);

//Set the amount of time to add (in seconds)

durToAdd = new GlideDuration(timeToAdd*1000);

var newDateTime = sched.add(gdt, durToAdd, '');

1 ACCEPTED SOLUTION

Hi,

I have given you the serer script code. so here is how you should place it in SI

Script Include

var ajaxDateDiff = Class.create();
ajaxDateDiff.prototype = Object.extendsObject(AbstractAjaxProcessor, {
check5Days: function () {
  var valid = this.getParameter('sysparm_cdt');
  var startDate = new GlideDateTime();
  var days = 7;
  var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
  var schedule = new GlideSchedule();
  schedule.get("your_schedulesysid");
  var end = schedule.add(startDate, dur);

  var diffSeconds = gs.dateDiff(end, valid, true);
  if (diffSeconds < 0 )
  {
    return 'true';
  }
  else
  {
    return 'false';
  }
},
});

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

View solution in original post

8 REPLIES 8

Thats strange.

Can you check the schedule and see if its excluding weekdays or not.

Also add this line after var end = schedule.add(startDate, dur) and share the output

gs.log("date after add is "+end);

Here's what worked for me

var ajaxDateDiff = Class.create();
ajaxDateDiff.prototype = Object.extendsObject(AbstractAjaxProcessor, {
check5Days: function () {
var valid = this.getParameter('sysparm_cdt');
var startDate = new GlideDateTime();
var days = 7;
var dur = new GlideDuration(days*43200*1000);

var schedule = new GlideSchedule('2218ff1bdba8eb40fb6e753a8c96198d');
// schedule.get("2218ff1bdba8eb40fb6e753a8c96198d");
var end = schedule.add(startDate, dur);

var diffSeconds = gs.dateDiff(end, valid, true);
if (diffSeconds < 0 )
{
return 'true';
}
else
{
return 'false';
}
},
});

My count was days * 86400 * 1000. yours was half of it. Anyways, if it worked, thats great.

Kindly mark my comment as a correct answer if that helps you to solve the problem so that the question is moved to the solved list.

Regards,
Asif
2020 ServiceNow Community MVP

Thanks for marking the comment as helpful. Could you also mark my earlier comment (where the actual code is provided) as correct answer so that the question is moved to the solved list and others can benefit from it.

Regards,
Asif
2020 ServiceNow Community MVP