Show/Hide values in String field (choice is enabled) based on other field of same table

SnehalBudhalkar
Tera Contributor

How can I Show/Hide values in String field (choice is enabled, hence displayed like choice field) based on other field of same table. 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@SnehalBudhalkar 

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.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Shivalika
Mega Sage

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

Robert H
Mega Sage

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:

 

RobertH_0-1745761870469.png

 

And then in the Choices [sys_choice] table you use the "Dependent value" to link the choices:

 

RobertH_1-1745761995320.png

 

Regards,

Robert

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@SnehalBudhalkar 

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.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader