Lukasz Bojara
Kilo Sage

Back in the times when I was fresh in ServiceNow world, I was given a task to validate on the form if Start Date and End Date are valid, meaning that End Date must be after the Start Date. Back then it was a big task for me and I have spent a lot of time to figure out how to compare those two dates. Of course, I have ended up with my custom Script Include that was handling the comparison. But after some time I came into a conclusion that this functionality already exists in the platform, for sure on the Change Request form. I started digging and found a very quick way to validate start and end dates on any form. There is a UI Script called  StartEndDateValidation and here is how.

 

 

Two Quick Steps to Achieve End Date after Start Date Validation on Form

 

 

 

Step 1: Load the UI Script

 

Create an OnLoad Client Script that will load the StartEndDateValidation UI Script. If we will not do it we will not be able to use it, as it is not Global by default. Of course, we should check if such Client Script is not already created and is loading this UI Script.

 

The script should look like this:

 

function onLoad() {
   ScriptLoader.getScripts('StartEndDateValidation.jsdbx', function() {});
}

 

find_real_file.png

 

Step 2: Validate dates in an OnChange Client Script

 

Create an OnChange Client script that will react on End Date changes. As now we have the StartEndDateValidation UI Script available we can use the validateStartDateBeforeEndDate function. The script will look like this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   var showErrorMsg = function(errorMsg){
		g_form.showErrorBox("u_end_date",errorMsg);
	};

	g_form.hideFieldMsg("u_end_date", true);
	validateStartDateBeforeEndDate("u_start_date", "u_end_date", showErrorMsg);
	
}

 

find_real_file.png

 

 

The result



Whenever a user will choose the End Date that will be before Start Date an error message will be displayed.

find_real_file.png



This is a very simple solution and probably already known for experienced developers, it also has some flaws but it can save some time for people that are starting their journey with ServiceNow.

Comments
nherm
Tera Expert

Hi, I want to use that for table change_task as well. In classic view everything works fine. But when I switsch to Service Operations Workspace or CSM Workspace this funktion is not working. Do you have any idea, why?

Best regards Nicole

Reddy31
Tera Contributor

Hello - Can someone help me how can we acheive to empty the end_date field and make unable to submit the ticket in the above functionality.?

 

Reddy31
Tera Contributor
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   var showErrorMsg = function(errorMsg){
		// if you want to populate error message and clear the filed 
                  g_form.addErrorMessage('Please select end date after start date');
                  g_form.clearValue('end_date');

	};

	g_form.hideFieldMsg("u_end_date", true);
	validateStartDateBeforeEndDate("u_start_date", "u_end_date", showErrorMsg);
	
}
Version history
Last update:
‎10-02-2019 11:26 AM
Updated by: