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.

Script Include Date Validation

Kamva
Giga Guru

Hi Developers,

I have a script include to validate dates (start date and end date) on a catalog item. The end date should be bigger than (or the date after) the start date, however, if my end date is on month after my start date (eg 13-06-2023) but the date is smaller than the date of my start date (eg 29-05-2023). The script deems the end date smaller than my start date. Please check the scripts and the attachments below and help me fix the problem.

 

Kamva_0-1685311961728.png

 

Kamva_1-1685311981372.png

 

Script Include:

var EndDate = Class.create();
EndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	endDate: function(){
		//GETTING DATES FROM THE UI
		var endDate = this.getParameter('sysparm_end_date');
		var startDate = this.getParameter('sysparm_start_date');
		
		//GETTING TODAYS DATE
		var _gdt = new GlideDateTime();
		var gdt = _gdt.getByFormat('dd-MM-yyyy');
		
		if (endDate == startDate){
			return 'Delegate should be created at least for 1 day';
		}else if (endDate < startDate){
			return 'End date should be after the start date selected';
		}
		
		gdt.addDaysUTC(30); //Highest possible date
		if(endDate < gdt){
			return 'Delegate can be created for 30 days at max';
		}
		return 'valid';
	},
	
    type: 'EndDate'
});

 

Catalog Client Script:

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

   //Type appropriate comment here, and begin script below	
	var gaStartDate = new GlideAjax('StartDate');
	gaStartDate.addParam('sysparm_name', 'startDate');
	gaStartDate.addParam('sysparm_start_date', g_form.getValue('starts'));
	gaStartDate.getXML(startDateParse);
	
	function startDateParse(response){
		var ans = response.responseXML.documentElement.getAttribute('answer');
		if (ans != 'valid'){
			g_form.addErrorMessage(ans);
			g_form.clearValue('starts');
		}
	}
}

 

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Kamva 

so if start date is 29th May then end date should be after it

I didn't get the extra validation

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

@Ankur Bawiskar it should be any date after 29 of May

@Kamva 

simply use this in onChange client script of end date

something like this and enhance it furter

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

	if(g_form.getValue('starts') != '' && newValue){
		var start = new Date(g_form.getValue('starts')).getTime();
		var end = new Date(newValue).getTime();
		if(end < start){
			var message = 'Please give valid start and end dates';
			g_form.showErrorBox('ends', message);
		}

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

Pravindra1
Tera Guru

Hello @Kamva ,

 

In the Catalog Client Script, I don't see any parameter for end date. GlideAjax has only Start date parameter passed.

 

 
   //Type appropriate comment here, and begin script below
var gaStartDate = new GlideAjax('StartDate');
gaStartDate.addParam('sysparm_name', 'startDate');
gaStartDate.addParam('sysparm_start_date', g_form.getValue('starts'));
//Parameter is missing for End Date
gaStartDate.getXML(startDateParse);