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.

Date onChange Client Script Issue

Community Alums
Not applicable

Hi,

I have a scenario where I have to see that start date should not be greater than end date. For this, I have created two client scripts. I am getting error here. Kindly help.

 

First Client Script

 

1.png

 

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

	if(newValue )
    var start = g_form.getValue('delegation_start');
    //g_form.addInfoMessage(start);
    var end = g_form.getValue('delegation_end');
    //g_form.addInfoMessage(end);
    if (start > end) {
        g_form.addErrorMessage('Delegation Start date cannot be after End date');
        g_form.clearValue('delegation_end');
    }
}

 

Second Client Script

 

2.png

 

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

	var start = g_form.getValue('delegation_start');
	//g_form.addInfoMessage(start);
	var end = g_form.getValue('delegation_end');
	//g_form.addInfoMessage(end);
	if(start > end){
		g_form.addErrorMessage('Delegation Start date cannot be after End date');
		g_form.clearValue('delegation_end');
	}


}

 

As soon as I select this,

 

3.png

 

I am getting the error 'Delegation Start date cannot be after End date'.

 

Regards

Suman P.

1 ACCEPTED SOLUTION

@Community Alums 

you need to use return false if validation fails

function onSubmit() {

    if ((g_form.getValue('delegation_start') != '') && (g_form.getValue('delegation_end') != '')) {
        var start = g_form.getValue('delegation_start');
        //g_form.addInfoMessage(start);
        var end = g_form.getValue('delegation_end');
        //g_form.addInfoMessage(end);
        if (start > end) {
            g_form.addErrorMessage('Delegation Start date cannot be after End date');
            g_form.clearValue('delegation_end');
			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

View solution in original post

6 REPLIES 6

Runjay Patel
Giga Sage

Hi @Community Alums ,

 

Use below onchange client script for both.

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

    var start = g_form.getValue('delegation_start');
    var end = g_form.getValue('delegation_end');

    if (start && end) { // Ensure both dates are present
        if (new Date(start) > new Date(end)) { // Compare Date objects
            g_form.addErrorMessage('Delegation Start date cannot be after End date');
            g_form.clearValue('delegation_end'); // Clear the invalid End Date
        }
    }
}

RunjayPatel_0-1735569730038.png

 

tested it, working as expected.

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Hi @Community Alums 

 

Thank you for marking my solution as helpful! The community now supports multi-solution acceptance, allowing you to accept multiple answers.