The Zurich release has arrived! Interested in new features and functionalities? Click here for more

onChange script recursion

reviest
Tera Contributor

I have a Catalog Item with multiple choice (radiobox) variable with 2 choices making visible/hidden different variable sets. 

Screenshot_8.jpg

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.

4 REPLIES 4

AnubhavRitolia
Mega Sage
Mega Sage

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]);
}
} 
}

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

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.

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. 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

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?