onChange script recursion
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2022 09:47 AM
I have a Catalog Item with multiple choice (radiobox) variable with 2 choices making visible/hidden different variable sets.
When a different choice is selected an onChange script is invoked. The script checks if any input to fields is present, and if it is, a popup appears asking if user wants to proceed and clear the input. I'm using confirm method for that.
The goal is to stay on the previous value in case 'Cancel' is pressed, but my script is throwing me into recursion I cannot seem to tackle. Here's my script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var fields = ['fieldA', 'fieldB', 'fieldC', 'fieldD'];
if (g_form.getValue('fieldA') != '' || g_form.getValue('fieldB') != '' || g_form.getValue('fieldC') != '' || g_form.getValue('fieldD') != '') {
var con = confirm("Changing request type will clear previous input.");
}
if (con == true) {
for (var i = 0; i < fields.length; i++) {
g_form.clearValue(fields[i]);
}
} else {
g_form.setValue('request_type', oldValue);
}
}
I understand that technically by setting the value back I'm re-invoking the script again, but I cannot think of anything else in here. I would very much appreciate any help on this one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2022 10:29 AM
Hi @reviest
You just need to remove Else condition as below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var fields = ['fieldA', 'fieldB', 'fieldC', 'fieldD'];
if (g_form.getValue('fieldA') != '' || g_form.getValue('fieldB') != '' || g_form.getValue('fieldC') != '' || g_form.getValue('fieldD') != '') {
var con = confirm("Changing request type will clear previous input.");
}
if (con == true) {
for (var i = 0; i < fields.length; i++) {
g_form.clearValue(fields[i]);
}
}
}
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2022 10:43 AM
Thank you for your reply. When else condition is removed selection doesn't stay with previous choice, which is required. It selects a different choice and makes a different variable set visible, but I need it to stay with a previous one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2022 10:59 AM
Hi @reviest
Try below code:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '' || newValue!=oldValue) {
return;
}
var fields = ['fieldA', 'fieldB', 'fieldC', 'fieldD'];
if (g_form.getValue('fieldA') != '' || g_form.getValue('fieldB') != '' || g_form.getValue('fieldC') != '' || g_form.getValue('fieldD') != '') {
var con = confirm("Changing request type will clear previous input.");
}
if (con == true) {
for (var i = 0; i < fields.length; i++) {
g_form.clearValue(fields[i]);
}
} else {
g_form.setValue('request_type', oldValue);
}
Just added "newValue!=oldValue" condition in 2nd line.
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2022 11:22 AM
Even with this condition added it still changes selection to a Choice B, which I want to prevent altogether. However, It's now easier to come back to Choice A since it ends the script after a new check you proposed.
Any ideas how can I keep Choice A selected after pressing cancel?