- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2025 05:38 AM
How can I Show/Hide values in String field (choice is enabled, hence displayed like choice field) based on other field of same table.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2025 07:22 AM
If the other field on same table is string and not choice then you will have to use onChange client script to add/remove the options, something like this but please enhance as per your requirement
// onChange client script for fieldA
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Define the options mapping
var optionsMapping = {
'value1': [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' }
],
'value2': [
{ value: 'option3', label: 'Option 3' },
{ value: 'option4', label: 'Option 4' }
],
'default': [
{ value: 'default', label: 'Default Option' }
]
};
// Clear existing options in fieldB
g_form.clearOptions('fieldB');
// Get the options for the new value of fieldA
var options = optionsMapping[newValue] || optionsMapping['default'];
// Add the options to fieldB
options.forEach(function(option) {
g_form.addOption('fieldB', option.value, option.label);
});
}
OR
If the other field on same table is choice then you can make it dependent on it, similar to how incident subcategory is dependent on category, approach already shared by @Robert H
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
04-27-2025 05:43 AM
Hello @SnehalBudhalkar
You need to clear the values first and then add the values which you want to display based on field selection.
g_form.clearOptions() and g_form.addOption()
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2025 06:55 AM - edited 04-27-2025 06:56 AM
Hello @SnehalBudhalkar ,
You can configure a dependency between two choice fields in the Dictionary of the second field.
Here is an example from the OOTB Incident "Subcategory" field, which is depending on the "Category" field:
And then in the Choices [sys_choice] table you use the "Dependent value" to link the choices:
Regards,
Robert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2025 07:22 AM
If the other field on same table is string and not choice then you will have to use onChange client script to add/remove the options, something like this but please enhance as per your requirement
// onChange client script for fieldA
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Define the options mapping
var optionsMapping = {
'value1': [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' }
],
'value2': [
{ value: 'option3', label: 'Option 3' },
{ value: 'option4', label: 'Option 4' }
],
'default': [
{ value: 'default', label: 'Default Option' }
]
};
// Clear existing options in fieldB
g_form.clearOptions('fieldB');
// Get the options for the new value of fieldA
var options = optionsMapping[newValue] || optionsMapping['default'];
// Add the options to fieldB
options.forEach(function(option) {
g_form.addOption('fieldB', option.value, option.label);
});
}
OR
If the other field on same table is choice then you can make it dependent on it, similar to how incident subcategory is dependent on category, approach already shared by @Robert H
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