Date Validation in ServiceNow

Hello ServiceNow Geeks,

In this article, I will be covering the concept of date/time filed validation in ServiceNow. I will take a practical example of past and future date validation in ServiceNow.

At the end of this article, you will be able to solve practical questions like- how to restrict date field in ServiceNow, how to restrict past date in ServiceNow, date should not be past date, and date format validation in ServiceNow, etc.

Past Future Date Validation

I will start off with a very common example where we will learn how to restrict past date in ServiceNow. This requirement can be fulfilled using two ways

  1. Using UI Policy, with condition “date field before today” and put error messages in “Script” section of same UI policy. It is almost no code approach. It is valid on both date/time field as well as date type variable in catalog item.
  2. Using onChange client script and script include. This is best approach as it can cover very complex requirements as well. Also, its an ideal approach to cover even very simple requirement. This is “Not a no code approach“.

ExampleWe have date/time field with name ‘Resume time’ (u_resume_time) and we want to restrict its value to not past date/time & also should not be more than 6 months in the future

Write onChange client script with below code-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	var time = new GlideAjax('ResumeDateTime');
	time.addParam('sysparm_name', 'validateDateTime');
	time.addParam('sysparm_date', newValue);
	time.getXML(checkDateTime);
	
	function checkDateTime(response){
		var answer = response.responseXML.documentElement.getAttribute('answer');
		
		if(answer){
			alert('Resume time should not be past date/time & should not be more than 6 months in future');
			g_form.clearValue('u_resume_time');
		}
	}
}

Write a script include with client callable is true-

var ResumeDateTime = Class.create();
ResumeDateTime.prototype = Object.extendsObject(AbstractAjaxProcessor, {
/*Value of 'Resume time' field should not be past date/time & should not be more than 6 months in the future.*/
	validateDateTime : function(){
		var parmdata = this.getParameter('sysparm_date');
		var currentglidetime = gs.nowDateTime();
		var futureglidetime = new GlideDateTime().addMonthsLocalTime(6);
		
if(parmdata < currentglidetime || parmdata > futureglidetime){
			return true;
		}
	},
	
    type: 'ResumeDateTime'
});

I hope this code will help you in making a basic mindset how can we achieve date and time validation in servicenow effectively & easily using GlideDateTime().

Date Format Validation

In the next example, I am showing you how to do date format validation in ServiceNow successfully. Comparing two date/time fields in different formats can be easily tackled and solved using getDisplayValue() function. For example, we have two date/time field with formats yyyy-mm-dd HH:mm:ss and MM-dd-yyyy hh:mm:ss respectively. getDisplayValue() retrieves date/time in user’s time zone and display format. This helps us in doing date format validation in ServiceNow.

Important Note-

While declaring a GlideDateTime class constructor, do not use gs.nowDateTime() as its parameter because calling GlideDateTime() constructor itself returns current date time.

//Good to use
var currentTime = new GlideDateTime();

//Bad to use
var currentTime = new GlideDateTime(gs.nowDateTime());

Also, as per ServiceNow documentation gs.nowDateTime() method is not available in scoped applications.

Please do give your inputs if I missed something. Do share views by commenting and sharing this posts to all ServiceNow geeks out there, who are interested in date/time validation in ServiceNow.

Leave a Reply

Your email address will not be published. Required fields are marked *