- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2020 01:32 AM
Hello!
I have 2 variables X & Y.
Variable X is a Select Box with values A & B
Variable Y is a Select Box with values 1, 2, 3, 4, 5, 6
I want to achieve are:
When X = A, Y should only display 1, 2, 3
When X = B, Y should only display 4, 5, 6
How to do this?
Should I create 2 catalog client scripts for X = A and X = B?
Please help!
Thank you!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2020 02:08 AM
Hey,
I did same requirement on my instance and it is working right
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue == 'a'){
g_form.addOption('variable_y',1,1);
g_form.addOption('variable_y',2,2);
g_form.addOption('variable_y',3,3);
g_form.removeOption('variable_y',4,4);
g_form.removeOption('variable_y',5,5);
g_form.removeOption('variable_y',6,6);
}
else if(newValue == 'b'){
g_form.addOption('variable_y',4,4);
g_form.addOption('variable_y',5,5);
g_form.addOption('variable_y',6,6);
g_form.removeOption('variable_y',1,1);
g_form.removeOption('variable_y',2,2);
g_form.removeOption('variable_y',3,3);
}
//Type appropriate comment here, and begin script below
}
Thanks,
Shrutika
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2020 01:38 AM
Hi,
only 1 onChange client script on Variable X should suffice.
Sample script below
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
if(newValue === '')
g_form.clearOptions('variable_y');
return;
}
g_form.clearOptions('variable_y');
if(newValue == 'A'){
g_from.addOption('variable_y',1,1);
g_from.addOption('variable_y',2,2);
g_from.addOption('variable_y',3,3);
}
else if(newValue == 'B'){
g_from.addOption('variable_y',4,4);
g_from.addOption('variable_y',5,5);
g_from.addOption('variable_y',6,6);
}
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2020 01:55 AM
I updated your code to this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
if(newValue === '')
g_form.clearOptions('variable_y');
return;
}
g_form.clearOptions('variable_y');
if(newValue == 'A'){
g_from.addOption('variable_y',1,1);
g_from.addOption('variable_y',2,2);
g_from.addOption('variable_y',3,3);
g_from.removeOption('variable_y',4,4);
g_from.removeOption('variable_y',5,5);
g_from.removeOption('variable_y',6,6);
}
else if(newValue == 'B'){
g_from.removeOption('variable_y',1,1);
g_from.removeOption('variable_y',2,2);
g_from.removeOption('variable_y',3,3);
g_from.addOption('variable_y',4,4);
g_from.addOption('variable_y',5,5);
g_from.addOption('variable_y',6,6);
}
}
It works when a value is selected (ex. A). But when another value is selected without refreshing the form (ex. B), no option is displayed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2020 02:11 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2020 02:22 AM
Hi,
one more addition; when value changes you should clear previous options and then add the required options
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
if(newValue === '')
g_form.clearOptions('variable_y');
return;
}
g_form.clearOptions('variable_y');
if(newValue == 'A'){
g_form.clearOptions('variable_y');
g_from.addOption('variable_y',1,1);
g_from.addOption('variable_y',2,2);
g_from.addOption('variable_y',3,3);
}
else if(newValue == 'B'){
g_form.clearOptions('variable_y');
g_from.addOption('variable_y',4,4);
g_from.addOption('variable_y',5,5);
g_from.addOption('variable_y',6,6);
}
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader