The CreatorCon Call for Content is officially open! Get started here.

onchange script oldValue not working

Amit Dey1
Tera Contributor

hi , I have written below catalog client script but it is not working

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (oldValue == 'Yes' && newValue == 'No') {
alert('triggering');
g_form.clearValue('variable1');
g_form.clearValue('variable2');
}

I removed oldvalue part then alert is coming but when I removed newvalue part then this alert is not triggering , so that oldvalue is not working , please help me to fix it 

9 REPLIES 9

Prince Arora
Tera Sage

@Amit Dey1 ,

Could you please print the "oldValue" and "newValue" in client script and let me know what are the values coming.
And change the value of the field just to debug the issue and trace the values.

 

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

alert(oldValue + " " + newValue);
if (oldValue == 'Yes' && newValue == 'No') {
alert('triggering');
g_form.clearValue('variable1');
g_form.clearValue('variable2');
}


If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

only newValue is coming , when I am selecting Yes , getting alert of Yes , when selecting No getting alert No

@Amit Dey1 ,

Could you please share the screenshot of your complete on change script and variable type just to check whether you have missed something or not!

Try This:

 

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

alert(oldValue + " " + newValue);
if ((oldValue == 'Yes' || oldValue == Yes) && newValue == 'No') {
alert('triggering');
g_form.clearValue('variable1');
g_form.clearValue('variable2');
}

Regards,Shamma Negi

mike_allgire
Giga Guru

The value of the oldValue is only available after the oldValue has been saved on the server. This is per KB0711972 at ServiceNow.

Symptoms

When oldValue is used in onChange client scripts it always return empty value instead of the previous value. An example:

onChange client script is set to run on Short description field. When the client script triggered the oldValue is returning empty instead of what the user previously entered in the Short description field prior to saving the change.
Release

All releases
Cause

The change has not been saved yet so the previous value (oldValue) is always empty (assuming there's no value in the field prior to the change).
Resolution

The field in which the onChange client script is executing against needs to have a value saved in the database first before oldValue can work to return the previous value saved.

Example of when it returns a value:

1) Short description is saved with a value of "ABC".

2) When onChange client script is executed oldValue would return "ABC".

 

Example of when it does not return any value:

1) "ABC" is entered into short description field but it's not saved.

2) When onChange client script is executed oldValue would return "" (empty value).

 

In either case, both are expected behaviors.