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

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

you can use this single onSubmit rather than 2 onChange

 

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

	g_form.hideFieldMsg('delegation_start');
	g_form.hideFieldMsg('delegation_end');

	if(g_form.getValue('delegation_start') != '' && g_form.getValue('delegation_end')){
		var start = new Date(g_form.getValue('delegation_start')).getTime();
		var end = new Date(g_form.getValue('delegation_end')).getTime();
		if(end < start){
			var message = 'Delegation Start date cannot be after End date';
			g_form.showFieldMsg('delegation_start', message, 'error');
			g_form.showFieldMsg('delegation_end', message, 'error');
			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

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

if you still wish to have 2 onChange then use this

onChange of Delegation End

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

    g_form.hideFieldMsg('delegation_end');

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

    var startDt = new Date(getDateFromFormat(start, g_user_date_format)).getTime();
    var endDt = new Date(getDateFromFormat(end, g_user_date_format)).getTime();

    if (endDt < startDt) {
        g_form.showFieldMsg('delegation_end', 'Delegation Start date cannot be after End date', 'error');
        g_form.clearValue('delegation_end');
    }
}

onChange on Delegation Start

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

    g_form.hideFieldMsg('delegation_start');

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

    var startDt = new Date(getDateFromFormat(start, g_user_date_format)).getTime();
    var endDt = new Date(getDateFromFormat(end, g_user_date_format)).getTime();

    if (endDt < startDt) {
        g_form.showFieldMsg('delegation_start', 'Delegation Start date cannot be after End date', 'error');
        g_form.clearValue('delegation_start');
    }
}

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

Community Alums
Not applicable

Hi @Ankur Bawiskar,

 

I have used this script, it is giving the error as expected but it is still going to REQ. Kindly help.

 

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');
	}

	}


}

 

Regards

Suman P.

@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