End date Validation in catalog item form

ashok17
Tera Contributor

Hi All,

 

Please help me below requirement:

 

In the catalog item there are two fields for date:

 

State date and End date

The requirement was user can't choose End date filed more than one year from start date.

 

Please help me how we will write client script for this requirement.

 

Thank You.

 

1 ACCEPTED SOLUTION

Pavankumar_1
Mega Patron

Hi @ashok17 ,

If my response helps you to resolve the issue Accept the solution and close it. So that others can refer and get benefited.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

5 REPLIES 5

Kiran_45
Giga Guru

Hi Ashok,

You can use glide ajax for the same, in script include add 365 days to start date and define condition as end date should be less than of added date. Hope it helps!.

ashok17
Tera Contributor

Hi Kiran,

 

Thanks for response..!

Requesting if possible send me script for above requirement.

 

Thanks.

1) Write an on-change client script pass start date value to script include.

2) Use below code in script include 

 

Kiran_45_0-1688998790064.png

 Thanks

Pavankumar_1
Mega Patron

Hi @ashok17 ,

1. Create onchange client script and script include

Client script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	var ga = new GlideAjax('date_Validation'); //Name of the Script Include
	ga.addParam('sysparm_name', 'getDate'); //Name of the function in the script include
	ga.addParam('sysparm_start_date', g_form.getValue('start_date')); //Parameter to pass to the script include
	ga.addParam('sysparm_end_Date', g_form.getValue('end_date')); //Parameter to pass to the script include
	ga.getXMLAnswer(setdetails); //callback funtion
	function setdetails(response) {
		if(!response){
			g_form.clearValue('end_date');
			g_form.addInfoMessage('End should not be one year after start Date');
		}
	}
}

2. Script include: make sure you check the client calable chekc box.

var date_Validation  = Class.create();
date_Validation .prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getDate: function() {
		var start = this.getParameter('sysparm_start_date'); //Passing the start date from the client
		var end = this.getParameter('sysparm_end_Date'); //Passing the end date from the client
		var dif = gs.dateDiff(start, end, true); //Get the Different between datesin mins
		if(dif>0 && dif<=31536000){//365 days mins are equals 31536000
			return true;
		}
	},
	type: 'date_Validation '
});

Screenshot (828).png

Screenshot (827).png

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar