The CreatorCon Call for Content is officially open! Get started here.

other Category option are not visible based on script

Manohararuna
Tera Contributor

Hi Everyone,

I have used to below script to display Task categories(12 Category option) based  Three Assignment group(Workforce Management - APAC,/EU/US) ,But other Task categories(20) are  also not display to other Assignment groups

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var allowedGroups = [
'Workforce Management - EU',
'Workforce Management - APAC',
'Workforce Management - US'
];

var grpDisplay = g_form.getDisplayBox('assignment_group').value;
g_form.removeOption('u_task_category');

if (allowedGroups.indexOf(grpDisplay) > -1) {
g_form.addOption('u_task_category', 'Configuration', 'Configuration');
g_form.addOption('u_task_category', 'Login', 'Login');
g_form.addOption('u_task_category', 'NSO', 'NSO');
g_form.addOption('u_task_category', 'SFTP - Global scape', 'SFTP - Global scape');
g_form.addOption('u_task_category', 'Access update', 'Access update');
g_form.addOption('u_task_category', 'Query', 'Query');
g_form.addOption('u_task_category', 'Terminate', 'Terminate');
g_form.addOption('u_task_category', 'Interface', 'Interface');
g_form.addOption('u_task_category', 'Timeclock', 'Timeclock');
g_form.addOption('u_task_category', 'Store close', 'Store close');
g_form.addOption('u_task_category', 'schedule upload', 'schedule upload');
g_form.addOption('u_task_category', 'Holiday', 'Holiday');
g_form.addOption('u_task_category', 'Reports -WFC', 'Reports -WFC');
g_form.addOption('u_task_category', 'Reports -WFAN', 'Reports -WFAN');
g_form.addOption('u_task_category', 'Automate', 'Automate');
g_form.addOption('u_task_category', 'Auto Scheduling', 'Auto Scheduling');
g_form.addOption('u_task_category', 'Forecasting', 'Forecasting');
g_form.addOption('u_task_category', 'Batch Jobs', 'Batch Jobs');
g_form.addOption('u_task_category', 'Automate', 'Automate');

} else {
g_form.addOption('u_task_category', '', '');
}
}.Please tell the problem of this script
5 REPLIES 5

Rafael Batistot
Kilo Patron

Hi @Manohararuna 

This line is the problem

else {
    g_form.addOption('u_task_category', '', '');
}

 

If you have a default set of categories that should be visible to other assignment groups (e.g., 20 categories), then you need to explicitly add those in the else block.

else {
    // Add default task categories here
    g_form.addOption('u_task_category', 'General', 'General');
    g_form.addOption('u_task_category', 'Incident', 'Incident');
    // Add the rest of your 20 categories
}



If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

M Iftikhar
Giga Sage

Hi @Manohararuna 

 

Problem with your script is when assignment_group is not one of the "Workforce Management" groups, the code falls into this else block:

g_form.addOption('u_task_category', '', '');

This line adds a single, empty option to the u_task_category dropdown. It doesn't add any of the "20 other Task categories" that they mentioned.

 

Here's the updated onChange client script I tested on my instance for incident table:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    // Get the display value of the assignment group
    var grpDisplay = g_form.getDisplayBox('assignment_group').value;

    var workforceGroups = [
        'Hardware',
        'Software',
        'Service Desk'
    ];

    // Define categories for Workforce Management groups
    var wfmCategories = [
        {value: 'Configuration', label: 'Configuration'},
        {value: 'Login', label: 'Login'},
        {value: 'NSO', label: 'NSO'},
        {value: 'SFTP - Global scape', label: 'SFTP - Global scape'},
        {value: 'Access update', label: 'Access update'},
        {value: 'Query', label: 'Query'},
        {value: 'Terminate', label: 'Terminate'},
        {value: 'Interface', label: 'Interface'},
        {value: 'Timeclock', label: 'Timeclock'},
        {value: 'Store close', label: 'Store close'},
        {value: 'schedule upload', label: 'schedule upload'},
        {value: 'Holiday', label: 'Holiday'},
        {value: 'Reports -WFC', label: 'Reports -WFC'},
        {value: 'Reports -WFAN', label: 'Reports -WFAN'},
        {value: 'Automate', label: 'Automate'}, // Note: "Automate" appears twice in your list, assumed intentional
        {value: 'Auto Scheduling', label: 'Auto Scheduling'},
        {value: 'Forecasting', label: 'Forecasting'},
        {value: 'Batch Jobs', label: 'Batch Jobs'},
        {value: 'Automate', label: 'Automate'} // Second instance
    ];

    // Define categories for all other groups (your "20 other categories")
    var otherCategories = [
        {value: 'General Inquiry', label: 'General Inquiry'},
        {value: 'System Issue', label: 'System Issue'},
        {value: 'Software Request', label: 'Software Request'},
        {value: 'Hardware Request', label: 'Hardware Request'},
        {value: 'Network Issue', label: 'Network Issue'},
        {value: 'Printer Problem', label: 'Printer Problem'},
        {value: 'Password Reset', label: 'Password Reset'},
        {value: 'Application Support', label: 'Application Support'},
        {value: 'Data Access', label: 'Data Access'},
        {value: 'Email Issue', label: 'Email Issue'},
        {value: 'Collaboration Tools', label: 'Collaboration Tools'},
        {value: 'Security Incident', label: 'Security Incident'},
        {value: 'New User Setup', label: 'New User Setup'},
        {value: 'User Deactivation', label: 'User Deactivation'},
        {value: 'Mobile Device Support', label: 'Mobile Device Support'},
        {value: 'VPN Access', label: 'VPN Access'},
        {value: 'Cloud Service Issue', label: 'Cloud Service Issue'},
        {value: 'Database Support', label: 'Database Support'},
        {value: 'Reporting Issue', label: 'Reporting Issue'},
        {value: 'Training Request', label: 'Training Request'}
    
    ];

    g_form.clearOptions('u_task_category');
    g_form.addOption('u_task_category', '', '-- Select --'); 

    if (workforceGroups.indexOf(grpDisplay) > -1) {
        // Populate WFM categories if the group is one of the specified workforce groups
        for (var i = 0; i < wfmCategories.length; i++) {
            g_form.addOption('u_task_category', wfmCategories[i].value, wfmCategories[i].label);
        }
    } else {
        // Populate other categories for all other assignment groups
        for (var j = 0; j < otherCategories.length; j++) {
            g_form.addOption('u_task_category', otherCategories[j].value, otherCategories[j].label);
        }
    }
}

Now you can see when I'm selecting three specified groups (I chose Hardware, Software, and Service Desk for testing), specifc categories are being shown:

MIftikhar_0-1760435907196.pngMIftikhar_1-1760435953113.png

and when I'm selecting any of the other groups, other categories are being shown:

MIftikhar_2-1760436085670.pngMIftikhar_3-1760436106702.png

 

If my response helped, please mark it as the accepted solution and helpful so others can benefit as well.

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

Ankur Bawiskar
Tera Patron
Tera Patron

@Manohararuna 

update as this

  • Add the standard 20 categories when the assignment group is not one of the allowed WFM groups.
  • Only clear (remove) all options before adding, but always populate with the right set for the current group.
  • Avoid duplicate category entries.
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue === '') {
    return;
  }

  var allowedGroups = [
    'Workforce Management - EU',
    'Workforce Management - APAC',
    'Workforce Management - US'
  ];

  var wfmCategories = [
    'Configuration', 'Login', 'NSO', 'SFTP - Global scape', 'Access update', 'Query',
    'Terminate', 'Interface', 'Timeclock', 'Store close', 'schedule upload', 'Holiday',
    'Reports -WFC', 'Reports -WFAN', 'Automate', 'Auto Scheduling', 'Forecasting', 'Batch Jobs'
  ]; // Use only 18 unique values

  var otherCategories = [
    // Add the other 20 standard categories here
    'Category A', 'Category B', 'Category C', //...etc
  ];

  // Remove previous options before adding
  g_form.removeOption('u_task_category');

  // Use full category lists, not just blank option for other assignment groups
  var grpDisplay = g_form.getDisplayBox('assignment_group').value;

  if (allowedGroups.indexOf(grpDisplay) > -1) {
    wfmCategories.forEach(function(cat) {
      g_form.addOption('u_task_category', cat, cat);
    });
  } else {
    otherCategories.forEach(function(cat) {
      g_form.addOption('u_task_category', cat, cat);
    });
  }
}

šŸ’” If my response helped, please mark it as correct āœ… and close the thread šŸ”’ā€” this helps future readers find the solution faster! šŸ™

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

before select any group it showing None, but after selecting any assignment group then group related task category auto populated .But i want None option every time along with Option like if select Workforce Management group then Task category options showing with None

same way other assignment group related options also

is this below condition correct to show None option-

_form.clearOptions('u_task_category');
    g_form.addOption('u_task_category', '', '-- None --');
   
if (newValue === 'None') {
        return; // Only show the "None" option
    }