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 is greater than OR equal to the start date

SivaKumar123
Tera Contributor

End date is greater than OR equal to the Start date, I have used below script its not working for Equal function.

only working on the greater than function.

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var end = g_form.getValue('start_date');
    var start = g_form.getValue('end_date');

 

    if (end >= start) {
        alert("Start date is greater than OR equal to End date.");
        g_form.clearValue('end_date');
    }

}

1 ACCEPTED SOLUTION

@SivaKumar123 

same script just modify

1) onChange client script on End date

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

	g_form.hideErrorBox('start_date');
	g_form.hideErrorBox('end_date');

	if(g_form.getValue('start_date') != '' && g_form.getValue('end_date')){
		var start = new Date(g_form.getValue('start_date')).getTime();
		var end = new Date(g_form.getValue('end_date')).getTime();
		if(end < start){
			var message = "Start date should be greater than OR equal to End date.";
			g_form.showErrorBox('end_date', message);
			g_form.clearValue('end_date');
		}
	}
}
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Weird
Mega Sage

What is your question?
In your script you define variables "end" and "start", but use "beginning" in your comparison.

Hi Weird,

 

I have updated the start, still same error.

My question is 

Start date is greater than OR equal to the End date
 

Ankur Bawiskar
Tera Patron
Tera Patron

@SivaKumar123 

you better use onSubmit client script.

If you use onChange then you need to write 2 onchange. 1 on start and 1 on end

Sample onSubmit script

function onSubmit() {
	//Type appropriate comment here, and begin script below

	g_form.hideErrorBox('start_date');
	g_form.hideErrorBox('end_date');

	if(g_form.getValue('start_date') != '' && g_form.getValue('end_date')){
		var start = new Date(g_form.getValue('start_date')).getTime();
		var end = new Date(g_form.getValue('end_date')).getTime();
		if(end < start){
			var message = '"Start date should be greater than OR equal to End date.';
			g_form.showErrorBox('start_date', message);
			g_form.showErrorBox('end_date', message);
			return false;
		}

	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur,

 

Its working fine, but i need Onchange catalog script.

Kindly provide Onchange related one.

Thank you!