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

shloke04
Kilo Patron

Hi,



You can achieve this with the help of an simple Script Include and an On Change Catalog Client Script as mentioned below:



1) Create a Script Include with script as mentioned below:



var DateValidation = Class.create();


DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  validateDate3: function() {


  var ActualDate = this.getParameter('sysparm_date');


  return gs.dateDiff(gs.now(),ActualDate, true)/86400;


  },





      type: 'DateValidation'


});



find_real_file.png



2) Once the Script Include has been created then create a On Change Catalog Client Script on your Record Producer as mentioned below:



Script:



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


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


  return;


  }


  var ga = new GlideAjax('DateValidation');


  ga.addParam('sysparm_name','validateDate3');


  ga.addParam('sysparm_date',g_form.getValue('date'));   //Replace your Variable Name here in place of "date"


  ga.getXML(ProcessResult);




  function ProcessResult(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  if (answer > 0)


  {


  g_form.clearValue('date');                                                             //Replace your Variable Name here in place of "date"


  alert('Date should not be in the Future.');


  }


  }


  //Type appropriate comment here, and begin script below



}



find_real_file.png



In the above screen shot "Date(date)" is the Variable Name for me. Replace with your Variable Name as mentioned above:



Result:



find_real_file.png



And it also clears out the Value of the Date Variable after the popup.



Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hi Shloke,



I've just tried to implement the above, however I am not getting the popup message or the field clearing.   Can you see where I have gone wrong?



find_real_file.png


find_real_file.png


My Bad. Forgot to mention that you need to check the "Client Callable" checkbox as true on the script include form as shown below:



find_real_file.png



Please marked the above checkbox as true and try the same script again.



Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hi Shloke,



That makes sense, my mistake.   I've just ticked this and re-tried but still not working?


find_real_file.png


That's strange. It's working fine for me on my instance. Can you try the below steps by debugging our script to check what is coming in the logs:



1) In you script Include can you pass a Log statement and check the value as mentioned below:



Script:



var DateValidation = Class.create();


DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  validateDate3: function() {


  var ActualEndDate = this.getParameter('sysparm_date');


  var res = gs.dateDiff(gs.now(),ActualEndDate, true)/86400;


  gs.log('Value of res is' + res);


    return res;


  },





      type: 'DateValidation'


});



find_real_file.png.



2) Please update your client script as below and also pass the alert statement to check for the value getting rendered on the client side:



Script:



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


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


  return;


  }


  var ga = new GlideAjax('DateValidation');


  ga.addParam('sysparm_name','validateDate3');


  ga.addParam('sysparm_date',g_form.getValue('date'));


  ga.getXML(ProcessResult);



  //Type appropriate comment here, and begin script below



}




function ProcessResult(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  if (answer > 0)


  {


  alert(answer);


  g_form.clearValue('date');


  alert('Date should not be in the Future.');


  }


}



find_real_file.png



Please check and let me know the outcomes of the Log statement and alert Statement.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke