Simple DateTime checks with ServiceNow Script Includes

I can't remember the exact reason I created this script include, but after finding it figured I'd draft up a couple quick examples as the logic could be expanded to other checks, plus, I might need it again one day so its good to have on hand.

The script include detailed below creates a new class type in ServiceNow that can be used both from the client-side with Ajax, and from the server-side as a simple object call. Examples are included below for both the server and client-side use cases.

This include allows you to pass in a string with the data, or the date and time, and quickly check to see if it is in the future or the past. The client-side example shows how to raise an error if someone tries to update the Discovery Date to the future (can't predict the future, so its not valid).

Creating the Script Include

As is visible in the screenshot below, the box for Client callable has been checked, which extends the object from global.AbstractAjaxProcessor. This allows it to be called by Ajax and to pass parameters in via getParameter.

QuickDateCheck Script Include

As I wanted the script include to be used for both client-side and server-side operations a small block of code needed to be added at the beginning of each function.

When any of the functions run it checks the date_string value that was passed into the call, this will be populated when using the method server-side. If date_string is null, we attempt to pull data with the getParameter method, client-side calls will use this route

var QuickDateCheck = Class.create();QuickDateCheck.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {isFutureDate: function (date_string) {// Check for server side parameter, then for a AJAX oneif (!date_string){date_string = this.getParameter('sysparm_test_date');if(!date_string){return false;}}var testDate = new GlideDate();testDate.setDisplayValue(date_string);return testDate.after(new GlideDate());},isPastDate: function (date_string) {// Check for server side parameter, then for a AJAX oneif (!date_string){date_string = this.getParameter('sysparm_test_date');if(!date_string){return false;}}var testDate = new GlideDate();testDate.setDisplayValue(date_string);return testDate.before(new GlideDate());},isFutureDateTime: function (date_string) {// Check for server side parameter, then for a AJAX oneif (!date_string){date_string = this.getParameter('sysparm_test_date');if(!date_string){return false;}}var testDate = new GlideDateTime();testDate.setDisplayValue(date_string);return testDate.after(new GlideDateTime());},isPastDateTime: function (date_string) {// Check for server side parameter, then for a AJAX oneif (!date_string){date_string = this.getParameter('sysparm_test_date');if(!date_string){return false;}}var testDate = new GlideDateTime();testDate.setDisplayValue(date_string);return testDate.before(new GlideDateTime());},type: 'QuickDateCheck'});

Server-side Sample

gs.debug("The current time is: " + (new GlideDateTime()))quickCheckObject = new QuickDateCheck();gs.debug('2019-08-31 is past: ' + quickCheckObject.isPastDate('2019-08-31'))gs.debug('2019-08-31 is future: ' + quickCheckObject.isFutureDate('2019-08-31'))gs.debug('2019-09-09 20:04:46 is past time: ' + quickCheckObject.isPastDateTime('2019-09-09 20:04:46'))gs.debug('2019-09-09 20:04:46 is future time: ' + quickCheckObject.isFutureDateTime('2019-09-09 20:04:46'))

Boolean output from the function

Client-side Sample via Client Script

This client side sample displays an error message if someone tries to pick a date in the future for the Most Recent Discovery field. If an invalid date is picked, it clears the field.

Discovery Must Be In Past UI Script

function onChange(control, oldValue, newValue, isLoading)  {// This is important to prevent loop-like behaviorif (isLoading || newValue == '') {return;}var ga = new GlideAjax('QuickDateCheck');ga.addParam('sysparm_name','isPastDateTime');ga.addParam('sysparm_test_date', newValue);ga.getXMLAnswer(checkAllowed);// Based on our script include, inform the userfunction checkAllowed(answer){if (answer !== 'true') {g_form.addErrorMessage('Discovery date can\'t be in the future');g_form.clearValue('last_discovered');}}}

g_form.addErrorMessage at work!

Previous
Previous

Tricks with the ServiceNow Filter Navigator

Next
Next

Getting Related Record Data on ServiceNow Forms