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

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

This worked like a charm!  It's much better than using client scripts...

Someday when time permits, I will change all the client scripts over to a UI Policy - thanks for the information - I have bookmarked your article.

Ankur Bawiskar
Tera Patron
Tera Patron

@rachelconstantino 

please try this; this worked for me.

Script Include: it should be client callable

var DateTimeAjax = Class.create();
DateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	checkDate: function () {
		var gdt = new GlideDateTime(this.getParameter('sysparm_date'));
		var nowTime = new GlideDateTime();
		if(gdt < nowTime)
			return 'Date is in past';
		else{
			var dt = new GlideDateTime();
			dt.addDaysUTC(30);
			if(gdt < dt){
				return 'Date is not after 30 days from current date';
			}
		}
		return '';
	},

	type: 'DateTimeAjax'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var cdt = g_form.getValue('target_date'); //First Date/Time field

var ga = new GlideAjax('DateTimeAjax');
    ga.addParam('sysparm_name', "checkDate");
    ga.addParam('sysparm_date', cdt); //
    ga.getXMLAnswer(function(answer){

        if(answer != ''){
            alert(answer); // remove this after testing

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

            g_form.clearValue('target_date');    
        }

    });

}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I tried this but it is allowing me to enter dates that are within 30 days from today..

Giles Lewis
Giga Guru

And, please also vote for https://community.servicenow.com/community?id=view_idea&sysparm_idea_id=79e1d7b1db2c8c185ed4a851ca96...

... a product enhancement to allow date validations in client scripts.