Show/Hide Variable Choices based on another variable

bijender1
Tera Contributor

Hi All,

I have two Multi choice variables in catalog item as below:

1 Variable named : Team -  have 5 choices

2nd Variable named: sub-team - have 20 choices

Requirement is to show 5 choices of second variable based on the first variable instead of all the choices of second variable.

Like : if first choice of Team is "IT" then second variable will show only 5 choice : ex: data Tea,/Security Team " only  

Regards,

1 ACCEPTED SOLUTION

AbhishekGardade
Giga Sage

Hello Bijender,

1. Create onChange client scrpt as below:

find_real_file.png

2 Add below code:

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {
return;
}

var teamName = g_form.getValue('team');
g_form.clearOptions('sub_team');

alert(teamName);
if (teamName == "it") {

g_form.addOption('sub_team', 'data', 'Data');
g_form.addOption('sub_team', 'tea', 'Tea');
}
else if (teamName == "not_it") {

g_form.addOption('sub_team', '--None--', '--None--');
g_form.addOption('sub_team', 'security', 'Security');

}

}

you can add multiple else if depending upon how much team you have.

Reference:

https://developer.servicenow.com/app.do#!/api_doc?v=london&id=r_GlideformAddOption_String_String_Str...

https://www.servicenowguru.com/scripting/client-scripts-scripting/removing-disabling-choice-list-opt...

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

View solution in original post

4 REPLIES 4

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi,

you can do it with UI Policy, please follow this article (it's related to a known issue however you can use the "Step to reproduce" for reproduce the same behavior):

https://hi.service-now.com/kb_view.do?sysparm_article=KB0678900

Hope this can help you.

If I have answered your question, please mark my response as correct and/or helpful so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.

Thank you

Cheers
Alberto

Hi Alberto,

Thanks, but UI Policy helps to show/hide a variable set/variable, my requirement is to show/hide the choice under a particular variable.

Ex: my variable have 20 choices and i need to show only 5 of them based on another variable selection.

Kindly advise,

Regards,
B

Sorry, my bad, then you have to write a Client script, check here an example, use this code as per your need:

https://community.servicenow.com/community?id=community_question&sys_id=a89e3665db58dbc01dcaf3231f96...

AbhishekGardade
Giga Sage

Hello Bijender,

1. Create onChange client scrpt as below:

find_real_file.png

2 Add below code:

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {
return;
}

var teamName = g_form.getValue('team');
g_form.clearOptions('sub_team');

alert(teamName);
if (teamName == "it") {

g_form.addOption('sub_team', 'data', 'Data');
g_form.addOption('sub_team', 'tea', 'Tea');
}
else if (teamName == "not_it") {

g_form.addOption('sub_team', '--None--', '--None--');
g_form.addOption('sub_team', 'security', 'Security');

}

}

you can add multiple else if depending upon how much team you have.

Reference:

https://developer.servicenow.com/app.do#!/api_doc?v=london&id=r_GlideformAddOption_String_String_Str...

https://www.servicenowguru.com/scripting/client-scripts-scripting/removing-disabling-choice-list-opt...

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade