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.

adding 6months to date selected between today's date to expiration date

pritimayee
Tera Contributor

Can anyone please help on the below requirement. I have date field variable named assignment date. Another date field variable is expiration date.

Users should be able to select the expiration date between today's date to assignment date + 6months.They should not be able to select past dates or more than 6 months.

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use onChange client script + GlideAjax to validate that

Something like this

Client Script:

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

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	// check if in past
	var today = new Date().getTime();
	var selectedDate = new Date(newValue).getTime();
	if(selectedDate < today){
		alert('Past date not allowed');
		g_form.clearValue('end_date');
	}
	// check for 6 months
	else if(oldValue != newValue){
		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_date');	
			}
		});
		//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'
});

Regards
Ankur

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use onChange client script + GlideAjax to validate that

Something like this

Client Script:

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

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	// check if in past
	var today = new Date().getTime();
	var selectedDate = new Date(newValue).getTime();
	if(selectedDate < today){
		alert('Past date not allowed');
		g_form.clearValue('end_date');
	}
	// check for 6 months
	else if(oldValue != newValue){
		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_date');	
			}
		});
		//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'
});

Regards
Ankur

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

@pritimayee1@gmail 

Glad to know that my script worked.

Please mark response helpful as well.

Regards
Ankur

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