Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to restrict date variable to only allow selecting a date that is 5 days from today and onwards?

alberttagle
Tera Contributor

Hi,

Please help me script.  I have date field named startdate, but I need for users not to be able to select any date before 5 days from today.

Thanks!

1 ACCEPTED SOLUTION

Here's my OnChange Client Script that calls the same Script Includes (with the original name however) requiring that a due date is greater than 5 days out (note it is in seconds):

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
 
 
  var cdt = g_form.getValue('due_date'); //First Date/Time field 
 
  var ajax = new GlideAjax('ClientDateTimeUtils'); 
  ajax.addParam('sysparm_name','getNowDateTimeDiff'); 
  ajax.addParam('sysparm_fdt', cdt); 
  ajax.getXML(doSomething); 
 
  function doSomething(response){ 
  var answer = response.responseXML.documentElement.getAttribute("answer"); 
  //5 days out
	  if (answer < 431000)
  {
  alert('The lead time to process this request is 5 days.' + '\n' +  'If this is an urgent request please contact the Service Desk for assistance escalating this request.');
	  //alert('The date value cannot be before 5 days into the future. Please correct.');
  //g_form.clearValue("due_date");
  }
  
  }
}

View solution in original post

6 REPLIES 6

Anurag Tripathi
Mega Patron
Mega Patron

Hii,

Refer the below link and you should be able to get the script

https://community.servicenow.com/community?id=community_question&sys_id=3d4b0321db9cdbc01dcaf3231f961935&view_source=searchResult

-Anurag

Hi,

I tried to make use of what I think is the answer, but without luck.  It doesn't work.

The bold characters below are the only ones I've modified. 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cdt = newValue; //The date field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.

/******** This Checks whether the selected date is in the Past or Not************************/
var ajax = new GlideAjax('ClientDateTimeUtils_New');
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);

function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer<-5){
alert("Start date can only be after 5 days from today.");
g_form.clearValue('startdate'); // Pass the variable name here
g_form.setMandatory('startdate', true);
}
}}

 

I have also created the script include, copied everything into it. Doesn't work.

Here's my OnChange Client Script that calls the same Script Includes (with the original name however) requiring that a due date is greater than 5 days out (note it is in seconds):

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
 
 
  var cdt = g_form.getValue('due_date'); //First Date/Time field 
 
  var ajax = new GlideAjax('ClientDateTimeUtils'); 
  ajax.addParam('sysparm_name','getNowDateTimeDiff'); 
  ajax.addParam('sysparm_fdt', cdt); 
  ajax.getXML(doSomething); 
 
  function doSomething(response){ 
  var answer = response.responseXML.documentElement.getAttribute("answer"); 
  //5 days out
	  if (answer < 431000)
  {
  alert('The lead time to process this request is 5 days.' + '\n' +  'If this is an urgent request please contact the Service Desk for assistance escalating this request.');
	  //alert('The date value cannot be before 5 days into the future. Please correct.');
  //g_form.clearValue("due_date");
  }
  
  }
}

Hi, Shane.

Thank you very much! It worked like a charm!