onChange client scripts not triggering if onChange field is changed programatically

NToo
Tera Expert

I made an observation and thought it worth noting here in the community.

We use Agent Workspace in our organisation, alongside NextUI.

 

I have an onChange client script that looks for changes on field A, and sets the value of field B.

Another onChange client script looks for changes on field B and performs an action.

 

I found that in Agent workspace, the second client script does not trigger when filed B is updated by the first script.

The second script triggers fine if the trigger is manual form manipulation.

 

In nextUI, the second script is triggered by the first without issue.

 

I thought it worth noting the observation in here, to see if anyone else has experienced similar.

I worked around it by merging the required logic into the first client script.

 


Steps to replicate:

Create an onChange client script that looks for changes on a field (A) and uses g_form.setValue to set field B

Create an onChange client script that looks for changes on field B

Observe that (in Agent workspace) script 2 only triggers when field B is manually changed, not when script 1 runs

 

 

2 ACCEPTED SOLUTIONS

@NToo 

if the 1st script is triggered via UI then 2nd should also trigger if it's onchange

Trigger for 1st script has to be from UI and then only it will work

Seems we are not able to get what you are trying to explain.

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

Thank you!

I have now managed to get to the bottom of this with your kind assistance.

Your working example helped me to recreate my own working example.

 

By doing this, I was able to focus in on my original scripts and pick apart any issues, knowing that the functionality should work.

 

What it turned out to be:

Script 1 was setting a field 

g_form.setValue('hold_reason', 1);
 
This was mistakenly setting 1 rather than string '1'.
 
This seemed to correctly set the value for the field visually, but as it hadn't been set correctly as a string, it was not triggering script 2.
 
Amending g_form.setValue('hold_reason', 1); to g_form.setValue('hold_reason', '1'); worked
 
Thank you so much for you support with this.
 

View solution in original post

14 REPLIES 14

Hi @NToo ,

 

So you are saying 2nd script is not event getting called in workspace view?

Did you try putting alert to check ?

Yes, thats correct. I tried removing all parts of the script, and just put an alert.

By doing this, I was able to confirm the 2nd script doesnt run.

 

The 2nd script runs fine if I manually change the field value by interacting with the form - I did this to confirm the script itself was not the issue but rather the trigger.

Which work space are you using? i tried in SOW while simple crating two onchange client script and it worked.

Hi @NToo ,

 

It should work in both, seems you have not selected UI Type = All.

RunjayPatel_0-1738057689221.png

 

Just now i have tested its working in both places if you keep UI Type=All.

 

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

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

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

 

NToo
Tera Expert

Hi Runjay,

I appreciate the response - this was indeed the first thing I checked and can confirm both scripts are set to All.

NToo_0-1738057832282.png