- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2022 01:45 AM
Hi Team,
I need to add and remove options based on country (select box) selection. When India, china, singapore, korea and germany is chosen "USB-A and USB-A Nano" should appear but when other country is chosen only "USB-C" should be displayed
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var loc = g_form.getValue('country');
// removed option by default
g_form.removeOption('preferred_form', '2');
g_form.removeOption('preferred_form', '1');
g_form.removeOption('preferred_form', '3');
if (loc == '8938b7111b121100763d91eebc0713ec' || '4d38b7111b121100763d91eebc0713eb' || '4938b7111b121100763d91eebc0713ed' || 'c538b7111b121100763d91eebc0713eb' || '9138b7111b121100763d91eebc0713f6') //India, china, singapore, korea and germany Sys_ID
{
g_form.addOption('preferred_form', '1','USB-A');
g_form.addOption('preferred_form', '3','USB-A Nano');
}
else if(loc != '8938b7111b121100763d91eebc0713ec' ||
'4d38b7111b121100763d91eebc0713eb' || '4938b7111b121100763d91eebc0713ed' || 'c538b7111b121100763d91eebc0713eb' || '9138b7111b121100763d91eebc0713f6')
{
g_form.addOption('preferred_form', '2','USB-C');
}
}
Regards,
Priyanka
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2022 02:04 AM
Hi,
So what's not working?
I would suggest this
1) clear all the options when onChange runs and then add the required options
Update as this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var loc = g_form.getValue('country');
var locationList = '8938b7111b121100763d91eebc0713ec,4d38b7111b121100763d91eebc0713eb,4938b7111b121100763d91eebc0713ed,c538b7111b121100763d91eebc0713eb,9138b7111b121100763d91eebc0713f6';
// removed option by default
g_form.clearOptions('preferred_form');
if (locationList.indexOf(loc) > -1) //India, china, singapore, korea and germany Sys_ID
{
g_form.addOption('preferred_form', '1','USB-A');
g_form.addOption('preferred_form', '3','USB-A Nano');
}
else{
g_form.addOption('preferred_form', '2','USB-C');
}
}
Regards
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
08-02-2022 02:44 AM
Hello Sumanth,
I mean, I want "none" option be there. I am aware of what you are saying but when addOption is added in the script it actually removes "None" option. How can we have that?
Regards,
Priyanka

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2022 03:20 AM
Yes. The drawback of using g_form.removeOptions() is that it will remove None also.
Solution is :
> First add None with the checkbox at variable level and
> You can try below script now
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.clearOptions('preferred_form');
if ((newValue == '8938b7111b121100763d91eebc0713ec') || (newValue == '4d38b7111b121100763d91eebc0713eb') || (newValue == '4938b7111b121100763d91eebc0713ed') || (newValue == 'c538b7111b121100763d91eebc0713eb') || (newValue == '9138b7111b121100763d91eebc0713f6') )//India, china, singapore, korea and germany Sys_ID
{
g_form.removeOption('preferred_form', '2','USB-C');
g_form.addOption('preferred_form', '1','USB-A');
g_form.addOption('preferred_form', '3','USB-A Nano');
}
else
{
g_form.removeOption('preferred_form', '1','USB-A');
g_form.removeOption('preferred_form', '3','USB-A Nano');
g_form.addOption('preferred_form', '2','USB-C');
}
}
Mark as correct and helpful if it solved your query.
Regards,
Sumanth

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2022 02:14 AM
Hi Priyanka,
You can update script as below
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.clearOptions('preferred_form');
if ((newValue == '8938b7111b121100763d91eebc0713ec') || (newValue == '4d38b7111b121100763d91eebc0713eb') || (newValue == '4938b7111b121100763d91eebc0713ed') || (newValue == 'c538b7111b121100763d91eebc0713eb') || (newValue == '9138b7111b121100763d91eebc0713f6') )//India, china, singapore, korea and germany Sys_ID
{
g_form.addOption('preferred_form', '1','USB-A');
g_form.addOption('preferred_form', '3','USB-A Nano');
}
else
{
g_form.clearOptions('preferred_form');
g_form.addOption('preferred_form', '2','USB-C');
}
}
Note : addOption, removeOption will not be applicable on Multiple Choice(Radio buttons)
Mark as correct and helpful if it solved your query.
Regards,
Sumanth