Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

End date should be greater than start date

vardhini
Kilo Expert

HI All,

I am using below code to make end date should be greater than start date, But it is working in reverse way like Its taking lesser date .

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
    
    var startDate=g_form.getValue('absence_start_date');
    if(newValue <= startDate )
        {
            g_form.clearValue('absence_end_date');
            g_form.showFieldMsg('absence_end_date', 'The last day of absence cant be before the first day.', 'error');
            
        }
    

   //Type appropriate comment here, and begin script below
   
}

 

Please check and update my code.

Thanks,

Raja

4 REPLIES 4

Not applicable

Hi @vardhini@45 ,

This thread should help you :

https://community.servicenow.com/community?id=community_question&sys_id=5010935ddb96c510019ac2230596...

Mark my answer correct & Helpful, if Applicable.

Thanks,
Sandeep

Ankur Bawiskar
Tera Patron

Hi,

You will have to write 2 onChange in this 1 for start and 1 for end

this should work fine in onChange

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

	g_form.hideFieldMsg('absence_end_date');

	if(g_form.getValue('absence_start_date') != '' && g_form.getValue('absence_end_date')){
		var start = new Date(g_form.getValue('absence_start_date')).getTime();
		var end = new Date(g_form.getValue('absence_end_date')).getTime();
		if(end < start){
			g_form.showFieldMsg('absence_end_date', 'The last day of absence cant be before the first day.', 'error');
		}
	}
}

Instead of this you can use single onSubmit client script

function onSubmit(){

	g_form.hideFieldMsg('absence_end_date');

	if(g_form.getValue('absence_start_date') != '' && g_form.getValue('absence_end_date')){
		var start = new Date(g_form.getValue('absence_start_date')).getTime();
		var end = new Date(g_form.getValue('absence_end_date')).getTime();
		if(end < start){
			g_form.showFieldMsg('absence_end_date', 'The last day of absence cant be before the first day.', 'error');
		}
	}
}

regards
Ankur

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

Hi Ankur,

I have used above code ,but its returning below error to me.

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

    g_form.hideFieldMsg('absence_end_date');

    if(g_form.getValue('absence_start_date') != '' && g_form.getValue('absence_end_date')){
        
        //alert("enter");
        var start = new Date(g_form.getValue('absence_start_date')).getTime();
        alert(start);
        
        var end = new Date(g_form.getValue('absence_end_date')).getTime();
        alert(end);
        if(end < start){
            
            
            g_form.showFieldMsg('absence_end_date', 'The last day of absence cant be before the first day.', 'error');
        }
    }

 

Kindly looksthis.

 

Regards,

Raja


}

image is broken

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