show/hide subcategory options based on an sssignment group

Jesus Nava
Tera Guru

Hello experts, please I need your expertise in this matter, I was asked to add more choices to the subcategory field which depend on a category selection. The subcategory field is a dependent field of the category.

I tried UI policy, client scripts with no luck, I checked other posts and try to use them but with no luck, can you guide me on how I can do it?

Here's the Client script I'm using

 

JesusNava_0-1752013631409.png

this is the UI policy, in when to run, I added the groups. (different alternative I used)

JesusNava_1-1752013715318.png

 

Thank you

4 REPLIES 4

GlideFather
Tera Patron

Hi @Jesus Nava,

 

in your script, there is if condition for the assignment group, if it is selected then some values are hidden.

 

if not selected then some choices are added.

 

you can try to build the condition as opposite - add the choices if this group is not selected and else to remove. So switch the logics with negation of the condition.


Reference qualifier is not helpful for this and what about decision table? 

Refer to this link

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0755115

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


Community Alums
Not applicable

Hi @Jesus Nava ,

i  will suggest use the client script 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    
    var assignmentGroup = g_form.getValue('assignment_group');
    
    // Clear existing options first to avoid duplicates
    g_form.clearOptions('subcategory');
    
    // Add base options that should always be available
    g_form.addOption('subcategory', 'base_option_1', 'General Request');
    g_form.addOption('subcategory', 'base_option_2', 'General Inquiry');
    
    // Add group-specific options
    if (assignmentGroup == '27f93fc47234e58a47ef6f746d43ie') { // IT Group
        g_form.addOption('subcategory', '108', 'Hardware Purchase');
        g_form.addOption('subcategory', '107', 'Software Purchase');
        g_form.addOption('subcategory', '109', 'Access Request');
    } 
    else if (assignmentGroup == 'another_sys_id_here') { // HR Group
        g_form.addOption('subcategory', '201', 'Benefits');
        g_form.addOption('subcategory', '202', 'Onboarding');
    }
    // Add more groups as needed
    
    // Optional: Set a default value
    g_form.setValue('subcategory', '');
}

 

 

If you prefer UI Policies:

  1. Create a UI Policy on the Category field

  2. Conditions: When Category changes

  3. Actions:

(function executeRule(current, previous) {
    var category = current.category.toString();
    var subcategory = current.subcategory;
    
    // Clear existing options
    subcategory.clearOptions();
    
    // Add options based on category
    if (category == 'IT') {
        subcategory.addOption('108', 'Hardware Purchase');
        subcategory.addOption('107', 'Software Purchase');
    }
    else if (category == 'HR') {
        subcategory.addOption('201', 'Benefits');
        subcategory.addOption('202', 'Onboarding');
    }
})(current, previous);

Ankur Bawiskar
Tera Patron
Tera Patron

@Jesus Nava 

client script looks good to me.

what debugging did you do?

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

Jesus Nava
Tera Guru

Thank you all for your support, I will try with the options you helped me out with and let you know my results,

 

Regards