Prevent an onChange Client Script from going into recursion
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2019 06:36 AM
I have an onChange client script called when a user selects an item for a field using the lookup functionality (here, referring to available CIs and user selects one of the CI from the lookup). If the user has selected a new CI item, then my client script is invoked which warns a user via a prompt on UI to confirm if he wants to go-ahead with the selected item or not? In this case if user confirms, then the new value is set in that field (new CI), else the field is set with the old value (old CI) itself.
In the above scenario, my script is going into recursion, as every item selection triggers an "onChange" event and eventually calling the client script. Would be of great help if someone can suggest a way of handling this or if there is any alternative solution here in terms of the best practices?
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2019 05:38 PM
This bit is causing the recursion, because you are setting the field which is calling the onchange again.
if (newValue === oldValue)
{
//There is no change. just return
g_form.setValue('cmdb_ci', oldValue);
return;
}
It needs to be like this
if (newValue === oldValue)
{
//There is no change. just return
return;
}
Whenever you set the value of the onChange field within its own onChange script, it will always call again.
You always need an execution path that returns without setting the value.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2019 05:20 PM
That should be fine.
Had a feeling a newValue / oldValue check would solve your issue!
Please mark my answer correct if recursion is no longer occurring, so others can use the answer to help them with recursion problems in Client Scripts 🙂
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2019 07:12 AM
This line might fix it, placed near the top:
if (newValue == oldValue) {
return;
}
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2019 10:13 AM
Hi,
I have posted my code in the above comment. Kindly do have a look.
Have used this condition but in a slightly different way. Do review the code and let me know your insight and whether any changes in it could help prevent recursion (if applicable).
Appreciate your help in this regard.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 05:43 PM
Unfortunately, "newValue == oldValue" didn't solve my problem.
I had a simple catalog client that needed to update the inputted value as uppercase:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '' || newValue == oldValue) {
return;
}
console.log("###AA on change..."); //see the recursion in the console
var test_var = g_form.getValue('test_var').toUpperCase();
if (newValue != test_var) { //prevented recursion
g_form.setValue('test_var', test_var);
}
//Type appropriate comment here, and begin script below
}
solution was from this: https://community.servicenow.com/community?id=community_question&sys_id=3d1143e5db98dbc01dcaf3231f961931
Also noticed that the recursion issue didn't seem to happen when using the Service Catalog from the Portal vs the Platform.
using this catalog client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '' || newValue == oldValue) {
return;
}
console.log("###AA on change...");
var test_var = g_form.getValue('test_var').toUpperCase();
//if (newValue != test_var) {
g_form.setValue('test_var', test_var);
//}
//Type appropriate comment here, and begin script below
}
In platform, we would see the recursion generating this error:
In Service Portal, we would not see a recursion: