Restrict a Date Field

rachelconstanti
Mega Sage

I have a requirement to restrict a date field.  This is for a Service Catalog Item.  The variable is target_date.  We need to set it up so that the target date selected cannot be in the past and needs to be 30 days or greater from the current date.

I have found some solutions on here but not are working.  

1 ACCEPTED SOLUTION

That's mentioned in the article, something like this would give you a message:

find_real_file.png

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

14 REPLIES 14

Hi Rachel,

The syntax for your script include is incorrect. You need to create a Client Callable Script Include (it should generate some code for you automatically when you enter a name) and then place the function inside it for future reference. Screenshot and code snippet below.

 

find_real_file.png

Script Include Code

var ClientCallableScriptInclude = Class.create();
ClientCallableScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	dateValidation: function() {
        return (gs.dateDiff(gs.nowDateTime(), new GlideDateTime(this.getParameter('sysparm_date')), true) / 3600 < 48);
    },

    type: 'ClientCallableScriptInclude'
});

 

The solution Mark has suggested is great an fits your purpose! Although you might find that you need to use a client script / script include combo in future if a more complex validation or calculation is needed, or the time that you wish to validate against isn't available from the dropdown field in the UI Policy.

 

I'd suggest giving this way a try on a personal Developer instnace just to get you familiar with GlideAjax calls, you'll find yourself using them in the future on the Service Portal no doubt for scenarios where you need to retreive some user data or create a Category / Subcategory relationship as you see on Incident form for example!

 

 Glad that you found a solution!

 

All the best,

Ethan

Priyanka Vasant
Tera Guru

Hello ,

Try the below script.

 

Script include :

Client callable: checked

Name:DateValidation

DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  validateDate: function() {

 

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

 

  return gs.dateDiff(gs.now(),ActualEndDate, true)/(86400*90); //90days

 

  },

   type: 'DateValidation'

 

});

Client Script:

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

 

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

 

          return;

 

    }

 

alert('on change started');

 

var ga = new GlideAjax('DateValidation');

 

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

 

  ga.addParam('sysparm_end_date',g_form.getValue('u_new_date')); //give your field name to be validated

 

  ga.getXML(ProcessResult);

 

 

 

  function ProcessResult(response) {

 

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

 

    alert(answer);

 

    if (answer > 1)

 

  {

 

  g_form.clearValue('u_new_date'); //give your field name to be validated

 

  alert('End Date should not be in the Past 90 days.');

  }

}

}

Mark helpful or correct based on impact.

Regards,

Priyanka

This prompted the alerts when I selected tomorrow for a date however after clearing the errors it let me submit with tomorrow's date.

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you considered using a (Catalog) UI Policy to achieve date validations? There's almost no-code needed to achieve date validations this way. Have a look at an article I wrote on this:
No Code date validations thru (Catalog) UI Policies

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

I tried this and was not successful...  I also need an "alert" message so that the person submitting knows they have to enter at least a date 30 days out...  Not sure this can be accomplished using a UI policy.