- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2017 06:25 AM
I have been tasked with configuring a field in change management to bounce up against black out dates. I know the system currently allows blackout dates, However, you need CI's to be used, and we have not reached that yet. I was wondering if someone has, or if there is a way, to go up against the ServiceNow Calendar for field validation?
Either one of 2 options
Option 1.
When user clicks on the calendar icon for "Planned start date" within Change, the calendar would refuse/black out any dates that are marked in the calendar as black out.
Option 2.
After selecting the "Planned start date", bounce it against the calendar to check if that date/time is blacked out?
Thanks
David.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2017 06:58 AM
Probably the most straightforward solution would be to warn the user when they pick a date that is in the blackout schedule. You can achieve this using a client callable script include. If you are unfamiliar with this, you should look over the documentation provided by ServiceNow:
To check a date against a schedule is pretty easy. All you need is the sys_id of the schedule you are checking against and the date you want to check. Here is a sample script that you can use in Scripts - Background to validate:
var chkDate = '2017-11-21 12:00:00';
var schedID = ''; //Put the sys_id of your blackout calendar within the quotes;
var chk = isWithinSched(chkDate, schedID);
gs.print(chk);
function isWithinSched(dateTime, schedID) {
// isWithinSched: function(dateTime, schedID) {
var sched = new GlideSchedule(schedID);
var chkDT = new GlideDateTime(dateTime);
return (sched.isInSchedule(chkDT));
// },
}
If you want to use the function within your script include, you can format the function using the commented out lines.
I hope this is helpful,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2017 07:11 AM
Hi Christopher, I hope this helps.
~~~~~~~~~~~~~~~~~~~~~~~
Script Include
~~~~~~~~~~~~~~~~~~~~~~~
var DateIsWithinSchedule = Class.create();
DateIsWithinSchedule.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isWithinSchedule: function () {
var schedID = this.getParameter('sysparm_schedID'),
dateTime = this.getParameter('sysparm_dateTime'),
sched = new GlideSchedule(schedID),
chkDT = new GlideDateTime(dateTime);
return sched.isInSchedule(chkDT);
}
});
~~~~~~~~~~~~~~~
UI Script . 3 message boxes are to show manager, for them to choose.
~~~~~~~~~~~~~~~
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var schedID = '385b08f9db07c300d7783c9b7c9619b1', // sys_id of the Change Blackout schedule
fieldName = 'start_date',
chkDate = g_form.getValue(fieldName),
ga = new GlideAjax('DateIsWithinSchedule');
if (isLoading || newValue === '')
return;
ga.addParam('sysparm_name', 'isWithinSchedule');
ga.addParam('sysparm_schedID', schedID);
ga.addParam('sysparm_dateTime', chkDate);
ga.getXML(DateIsWithinScheduleParse);
}
function DateIsWithinScheduleParse(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer == 'true') {
alert('The selected date lies within a change freeze period. Only Off Cycle or Emergency Changes are allowed during this freeze.'); // to show alerts popup
g_form.showFieldMsg('start_date', "The selected date lies within a change freeze period. Only Off Cycle and Emergency Changes are allowed during this freeze " , 'info'); //to show blue text
g_form.showFieldMsg('start_date', "The selected date lies within a change freeze period. Only Off Cycle and Emergency Changes are allowed during this freeze " , 'error'); //to show red text
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2017 08:16 AM
You may want to use the setDisplayValue() method to guarantee the GlideDateTime is in the user's time zone:
var DateIsWithinSchedule = Class.create();
DateIsWithinSchedule.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isWithinSchedule: function () {
var schedID = this.getParameter('sysparm_schedID'),
dateTime = this.getParameter('sysparm_dateTime'),
sched = new GlideSchedule(schedID),
chkDT = new GlideDateTime();
chkDT.setDisplayValue(dateTime);
return sched.isInSchedule(chkDT);
},
type: 'DateIsWithinSchedule'
});
This should work regardless of whatever time zone the user is in.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2017 09:11 AM
Thanks Christopher, that did it.