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 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

Community Alums
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
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  ||  9x 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader