- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2020 08:11 AM
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, '');
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2020 11:32 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2020 08:18 AM
Hi,
To add days to the schedule, check like this.
var startDate = new GlideDateTime(startDate);
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);
Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.
Regards,
Asif
2020 ServiceNow Community MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2020 09:19 AM
Hi Asif - so would I just add this to the end of the current client script?
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cdt=g_form.getValue('requested_by_date');
var ajax = new GlideAjax('ajaxDateDiff');
ajax.addParam('sysparm_name', 'check5Days');
ajax.addParam('sysparm_cdt',cdt);
ajax.getXML(showMessage);
var startDate = new GlideDateTime(startDate);
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);
}
function showMessage(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true')
{
g_form.setValue('expedite_request','yes');
}
}
Obviously changing to add my schedule sys_id? It's also a 12 hour a day schedule so the calculation may need adjusted on this script. so like:
new GlideDuration(days*604800*1000);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2020 11:32 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2020 05:11 AM
Asif - thanks for the response. I used the below code, however it's not using the schedule still. It's doing 7 calendar days instead of business days.
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("2218ff1bdba8eb40fb6e753a8c96198d");
var end = schedule.add(startDate, dur);
var diffSeconds = gs.dateDiff(end, valid, true);
if (diffSeconds < 0 )
{
return 'true';
}
else
{
return 'false';
}
},
});