onchange client script, oldValue is not coming in the alert.

Community Alums
Not applicable

Hi All,

Its a Yes/No catalog variable. I have written a onchange client script. The requirement is something like "if the particular field changes from Yes to No, do the logic". 

So i wrote like below, if (oldValue is Yes & newValue is No) but its not working. Even the alert in the 5th line is empty for oldValue, newValue alert is coming.

 

find_real_file.png

1 ACCEPTED SOLUTION

Hi,

then clear it only when the value selected is None

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading){
        return;
    }

    if(newValue == 'Yes'){
        g_form.setValue('hiddenVariable', 'Yes');
    }
    else if(newValue == ''){
        g_form.clearValue('hiddenVariable');
    }

    if(g_form.getValue('hiddenVariable') == 'Yes' && newValue == 'No'){
        // your logic
    }

}

Regards
Ankur

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

View solution in original post

18 REPLIES 18

@Sirraj 

I have shared the workaround below

Thank you for marking my response as helpful.

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

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

@Sirraj 

So to make your logic work do this

1) create hidden variable of type String and hide it always using UI policy on form load

2) whenever user selects Yes set the hidden variable with Yes

3) when user changes value from Yes clear it

4) now use this hidden variable value in your script for comparision

Script like this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading){
		return;
	}

	if(newValue == 'Yes'){
		g_form.setValue('hiddenVariable', 'Yes');
	}
	else if(newValue == 'No' ||  newValue == ''){
		g_form.clearValue('hiddenVariable');
	}

	if(g_form.getValue('hiddenVariable') == 'Yes' && newValue == 'No'){

		// your logic

	}

}

Regards
Ankur

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

Community Alums
Not applicable

When value changes from Yes to No, I need to do my logic.

So with this script, when value is Yes we are setting "hidden=yes", if value changes from (yes to no) hidden value will set to empty , because we are clearing the value (newValue = no)

Then in the if loop g_form.getValue('hidden') wil always empty only right? again our validation will fail.

Hi,

then clear it only when the value selected is None

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading){
        return;
    }

    if(newValue == 'Yes'){
        g_form.setValue('hiddenVariable', 'Yes');
    }
    else if(newValue == ''){
        g_form.clearValue('hiddenVariable');
    }

    if(g_form.getValue('hiddenVariable') == 'Yes' && newValue == 'No'){
        // your logic
    }

}

Regards
Ankur

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