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

The same script is working for me, you would give wrong variable names, please verify.


find_real_file.png


function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }


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','');


  }


}


    //Type appropriate comment here, and begin script below


   


}



find_real_file.png


Hope its onchange client script and you have selected the variable as date.



Please provide your code to review and proceed further.


Hi Balaji,



find_real_file.png



u_date is the name of the date/time variable that is being selected.   Can I confirm that I only needed to amend the variable name in line 5 and the same in line 19 or was there something else that I was missing?



Is there any other setup required in order to get GlideAjax to work as I'm having trouble on another question where some has suggested and given me code for a GlideAjax that is also not working?



Thanks


I can see the script is not active sate, active is false, make it true.


Hi Sam,



Did you tried my code by making client script Active true.


Hi Balaji,



I have tried with it set to active but it still on shows the message of the cdt variable, but does not warn of a future date and does not clear the field.   I've added below the client side as I have it set and the script includes, not sure what is the issue.



find_real_file.png


find_real_file.png


find_real_file.png


find_real_file.png