Restrict past or future dates more than 18 months

Snow-Man
Tera Contributor

Hello everyone,

 

I need to implement a validation rule for the knowledge article form. Specifically, if a user tries to enter a date in the "Valid to" field that is either in the past or more than 18 months into the future, an alert should be triggered. This alert should inform the user that entering dates outside of this range is not permitted.

 

Thank you!

2 ACCEPTED SOLUTIONS

Bert_c1
Kilo Patron

Hi @Snow-Man 

 

My PDI is offline currently, so I can't test. But I've used an onChange client script defined on the table for the 'Valid to' field, that calls a script include to perform the date math.

 

The script include logic:

 

 

 

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

	var newDate = g_form.getValue('valid_to');
	var ga = new GlideAjax('CheckValidDate');
	ga.addParam('sysparm_name', 'checkDate');
	ga.addParam('sysparm_newDate', newDate);
	ga.getXMLAnswer(getResponse);

	// callback function for returning the result from the script include
	function getResponse(response) {
		var result = response;
		alert("Response = " + result);		// for debug return value
		if (result  == 'false') {
			g_form.clearValue('valid_to');
			alert("entered date is 18 months before or after current date!");
		}
	} 
}

 

 

The script include has Client callable set to true, named 'CheckValidDate':

 

 

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

	checkDate: function() {
		var dateEntered = this.getParameter("sysparm_newDate");
		var newDateTime = new GlideDateTime(dateEntered);
		gs.info('CheckValidDate: checking ' + newDateTime);
		var gdt = new GlideDateTime();
		gdt.addMonthsLocalTime(18);
		if (newDateTime > gdt)
			return false;

		var gdt = new GlideDateTime();
		gdt.addMonthsLocalTime(-18);
		if (newDateTime < gdt)
			return false;

		return true;
	},

    type: 'CheckValidDate'
});

 

 

Again, I can't test the return value currently.

View solution in original post

Sumanth16
Kilo Patron

Hi @Snow-Man ,

 

 

Client Script:

Note: Ensure you give valid variable names for start and end dates

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

	var ga = new GlideAjax('DateTimeAjax');
	ga.addParam('sysparm_name', "compareDates");
	ga.addParam('sysparm_start', g_form.getValue('start_date')); // start variable
	ga.addParam('sysparm_end', g_form.getValue('end_date')); // end variable
	ga.getXMLAnswer(function(answer){
		if(answer != ''){
                        alert('End date should be within 6 months from start date');
			g_form.clearValue('end');	
		}
	});
	//Type appropriate comment here, and begin script below

}

Script Include: It should be client callable

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

	compareDates: function(){

		var start = new GlideDateTime(this.getParameter('sysparm_start'));
		var end = new GlideDateTime(this.getParameter('sysparm_end'));
		start.addMonthsUTC(6);

		if(end.getNumericValue() > start.getNumericValue()){
			return 'End Date should be withing 6 months from start';
		}
		return '';
	},

	type: 'DateTimeAjax'
});

 

or you can use No-code UI policy

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

View solution in original post

3 REPLIES 3

Bert_c1
Kilo Patron

Hi @Snow-Man 

 

My PDI is offline currently, so I can't test. But I've used an onChange client script defined on the table for the 'Valid to' field, that calls a script include to perform the date math.

 

The script include logic:

 

 

 

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

	var newDate = g_form.getValue('valid_to');
	var ga = new GlideAjax('CheckValidDate');
	ga.addParam('sysparm_name', 'checkDate');
	ga.addParam('sysparm_newDate', newDate);
	ga.getXMLAnswer(getResponse);

	// callback function for returning the result from the script include
	function getResponse(response) {
		var result = response;
		alert("Response = " + result);		// for debug return value
		if (result  == 'false') {
			g_form.clearValue('valid_to');
			alert("entered date is 18 months before or after current date!");
		}
	} 
}

 

 

The script include has Client callable set to true, named 'CheckValidDate':

 

 

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

	checkDate: function() {
		var dateEntered = this.getParameter("sysparm_newDate");
		var newDateTime = new GlideDateTime(dateEntered);
		gs.info('CheckValidDate: checking ' + newDateTime);
		var gdt = new GlideDateTime();
		gdt.addMonthsLocalTime(18);
		if (newDateTime > gdt)
			return false;

		var gdt = new GlideDateTime();
		gdt.addMonthsLocalTime(-18);
		if (newDateTime < gdt)
			return false;

		return true;
	},

    type: 'CheckValidDate'
});

 

 

Again, I can't test the return value currently.

Sumanth16
Kilo Patron

Hi @Snow-Man ,

 

 

Client Script:

Note: Ensure you give valid variable names for start and end dates

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

	var ga = new GlideAjax('DateTimeAjax');
	ga.addParam('sysparm_name', "compareDates");
	ga.addParam('sysparm_start', g_form.getValue('start_date')); // start variable
	ga.addParam('sysparm_end', g_form.getValue('end_date')); // end variable
	ga.getXMLAnswer(function(answer){
		if(answer != ''){
                        alert('End date should be within 6 months from start date');
			g_form.clearValue('end');	
		}
	});
	//Type appropriate comment here, and begin script below

}

Script Include: It should be client callable

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

	compareDates: function(){

		var start = new GlideDateTime(this.getParameter('sysparm_start'));
		var end = new GlideDateTime(this.getParameter('sysparm_end'));
		start.addMonthsUTC(6);

		if(end.getNumericValue() > start.getNumericValue()){
			return 'End Date should be withing 6 months from start';
		}
		return '';
	},

	type: 'DateTimeAjax'
});

 

or you can use No-code UI policy

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

Bert_c1
Kilo Patron

@Snow-Man 

 

I was able to test and made corrections to the client script posted earlier. It works as now posted for dates outside of 18 months. Seem the other post here didn't read what you asked for.