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.

Date/Time Field Restriction Record Producer

Sam Ogden
Tera Guru

Hi All,

I have a date/time field on a record producer asking when the incident first occurred.

I have seen that the only way yo restrict this so people cannot add a date in the future is to have a script which validates this between the current date and the date selected when the field changes and then have a pop up message to warn the user.

I'm just not sure on how to script this?

Any help is greatly appreciated.

Thanks

37 REPLIES 37

sachin_namjoshi
Kilo Patron
Kilo Patron

You can develop catalog client script for your record producer to implement validation for dates.



Please refer below post which will be helpful to you.



Date comparsion and validation within Catalog Items.



Regards,


Sachin


Nate23
Mega Guru

I think this is one of the best documented community posts on date/time validation on the client side.




Client Script Date/Time Functions



This should answer all your questions.


Hi Nathan,



Your code should be some thing like below


find_real_file.png


var cdt = g_form.getValue('date'); //First Date/Time field  
var dttype = 'minute'; //this can be day, hour, minute, second. By default it will return seconds.  
alert(cdt);
var ajax = new GlideAjax('ClientDateTimeUtils');  
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 >0)
{
  alert('date should not be future');
  g_form.setValue('date','');
}
}


Hi Sam,



You can try my code(above), its working.


we are using oob script include ClientDateTimeUtils.