onChange client scripts not triggering if onChange field is changed programatically

NToo
Tera Contributor

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

 

 

1 ACCEPTED SOLUTION

@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

14 REPLIES 14

@NToo 

it worked for me as well

Refer below

client script agent workspace.gif

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

Hi Ankur,

 

I believe that your test scenario is a script setting value of field based on change of a field by user input, which is slight different to my scenario.

 

My scenario is that that field that triggers the script, is changed by another script, therefore script 1 does not trigger script 2.

I'm not sure if I'm explaining that very well.

 

 

Script 1 : onChange that looks at 'state' field. When state changes, set Assignment group to X

 

Script 2: onChange that looks at 'Assignment group' field. When AG changed, set 'Assigned to' to Y

 

Triggering script 1 by changing the state, causes AG to be set to X, but script 2 is not triggered, therefore 'Assigned to' is not set.

 

Triggering script 2 manually by manually (user input) changing AG to X, correctly causes script 2 to trigger and 'Assigned to' is set

@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

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.
 

@NToo 

Glad that I was able to help you

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