OnChange Script running on Save/Update

guycouronn_
Kilo Contributor

Hi,

I'm using Geneva on my personal instance.

I have a onChange Client Script on State's Incident Table.

Here is my code :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

      if (isLoading || newValue === '' || oldValue === newValue) {

              return;

      }

   

      jslog("[ClosedBy Client Script] GC :   oldValue : "   + oldValue + " newValue : " + newValue);

}

When I'm editing an existing incident and change its state, I could see in javascript log this information :

17:38:04 (620)incident.do[ClosedBy Client Script] GC : oldValue : 6 newValue : 2

It's the expected behaviour.

However, after I changed its state, when I saved, there is another log :

17:38:11 (882)incident.do[ClosedBy Client Script] GC : oldValue : 6 newValue : 2

If I open an existing record, I change another field and save, there is no log.

If I change the state a multiple times before saving, the "after save" log is showing.

It seems that, when the onChange client script is called at last 1 time, it'll be called another time during the Save. About my condition, I know I could write an "global" variable which stores the previous value of form and return if it's not "really changed". However, it's not the problem, as the onChange should not be called on save.

Do you know if it's a know error ?

Thank you.

1 ACCEPTED SOLUTION

Hi,



Onchange scripts never runs at the time of submition of form. There can be only one reason: check the filed on which this script is running. There might be any onSUbmit script which refreshing the value of OnChange field, and due to that OnChange script can run.



Thanks,


Param


View solution in original post

4 REPLIES 4

The SN Nerd
Giga Sage
Giga Sage

The oldValue variable contains the value of the field when the form is loaded, not the last value of the field before it was changed.


Does this explain the behavior you are seeing?



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Hi,



Onchange scripts never runs at the time of submition of form. There can be only one reason: check the filed on which this script is running. There might be any onSUbmit script which refreshing the value of OnChange field, and due to that OnChange script can run.



Thanks,


Param


I completly forgot to test on another field, as State field's incident is used by many Scripts (BR/Client/etc.)



I tried on another field, created by me and it occurs only when it's changing, not onSubmit.



Thank you for this hint !


Yes I know, which is why I said :


"About my condition, I know I could write an "global" variable which stores the previous value of form and return if it's not "really changed"."



As the global variable could stores the previous value of the field, not the value of the field when the form is displayed.



However, I think it could be a good practice to verify if the value is changed from the "real" previous value's form and not only from the display value's form.