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.

end date cannot be before start date

Pettam Deepthi
Tera Contributor

Hi, I need a catalog client script on end date cannot be before start date and start date should be after 72 hours.

2 REPLIES 2

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

I think it is better to do it with UI policy as below:-

 

Saurav11_0-1668441189642.png

 

Saurav11_1-1668441237266.png

 

 

 

 

But if you want to do it via catalog client script do the below:-

 

Client Script:-

 

Replace the start date and end date variable name

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 more that 3 days 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.addDays(3);

		if(end.getNumericValue() < start.getNumericValue()){
			return 'End Date should be more tha 3 days from start';
		}
		return '';
	},

	type: 'DateTimeAjax'
});

 

Please mark my answer as correct based on Impact.

RodGallegos
Tera Guru

@Saurav11 Your answer is correct. @Pettam Deepthi  Please mark Saurav's answer as correct