- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 06:12 PM
Hello, I want to hide some choices but don't want to delete them.
Because some choice type fields are configrated to have dependencies.
Ex) [field_1]-choice_a, choice_b, choice_c
[field_2]-choice_d, choice_e *They will appear if choice_a are selected in [field_1].
Currently there are too many choices in one field and I would like to reduce them.
If you know how to make some of the choices invisible, please help me.
Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 07:14 PM
Hello @ClayS
To meet the requirement, write an onChange client script on field_1.
onChange Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return; // Do nothing if the form is loading or the field is empty
}
// Clear the options in field_2 to reset it
g_form.clearOptions('field_2');
// Check the value of field_1 and dynamically add options to field_2
if (newValue === 'choice_a') {
// Add the dependent options for choice_a
g_form.addOption('field_2', 'choice_d', 'Choice D');
g_form.addOption('field_2', 'choice_e', 'Choice E');
} else if (newValue === 'choice_b') {
// Add other dependent options for choice_b if needed
g_form.addOption('field_2', 'choice_f', 'Choice F');
g_form.addOption('field_2', 'choice_g', 'Choice G');
} else {
// Add a default placeholder if no matching options
g_form.addOption('field_2', '', '-- Select an option --');
}
}
Note: Update the option value and label as it is configured in your instance.
Refer the link to learn more about dynamically populating the choices
Using the g_form.addOption method
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 06:56 PM
Hi @ClayS
In onChange Client Script:
var value = g_form.getValue('field1');
if(value== choice_a){
g_form.addOption('field2', 'choice_d');
g_form.addOption('field2', 'choice_e');
}
Thanks
dgarad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 07:01 PM
you can remove options using this
g_form.removeOption('field_1', 'choiceValue1');
If my response helped please mark it correct and close the thread so that it benefits future readers.
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-22-2025 07:14 PM
Hello @ClayS
To meet the requirement, write an onChange client script on field_1.
onChange Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return; // Do nothing if the form is loading or the field is empty
}
// Clear the options in field_2 to reset it
g_form.clearOptions('field_2');
// Check the value of field_1 and dynamically add options to field_2
if (newValue === 'choice_a') {
// Add the dependent options for choice_a
g_form.addOption('field_2', 'choice_d', 'Choice D');
g_form.addOption('field_2', 'choice_e', 'Choice E');
} else if (newValue === 'choice_b') {
// Add other dependent options for choice_b if needed
g_form.addOption('field_2', 'choice_f', 'Choice F');
g_form.addOption('field_2', 'choice_g', 'Choice G');
} else {
// Add a default placeholder if no matching options
g_form.addOption('field_2', '', '-- Select an option --');
}
}
Note: Update the option value and label as it is configured in your instance.
Refer the link to learn more about dynamically populating the choices
Using the g_form.addOption method
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2025 02:13 AM
If I select 'Mouse' in the Devices field, only Kerala and Bangalore should be available in the Places field.
My code is not working: