Populate Unit Manager Groups

omatD
Mega Contributor

I have 2 fields unit_managers field (List Collector that refences the sys_user table) and requested_support_group field (Reference field that references the group table) in a catalog item in Service Portal

Whenever a requester selects a unit manager or more than one unit manager is selected in the unit_managers field the list of groups where the selected users are unit managers should be available (filtered) as options to select. The unit manager field on the group table is u_unit_managers.  I created an onChange client script and script include shown below but it still populates all the groups instead of the filtered groups

Client Script

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue) return;

// Get selected users from the List Collector
var unitManagers = g_form.getListCollectorValue('unit_manages');

// Clear if no managers selected
if (!unitManagers) {
g_form.clearOptions('requested_support_groups');
return;
}

// Call server-side Script Include
var ga = new GlideAjax('GroupManagerUtils');
ga.addParam('sysparm_name', 'getGroupsByUnitManagers');
ga.addParam('sysparm_managers', unitManagers);
ga.getXMLAnswer(function(response) {
try {
var groups = JSON.parse(response);
g_form.clearOptions('requested_support_groups');

if (groups.length > 0) {
groups.forEach(function(group) {
g_form.addOption('requested_support_groups', group.sys_id, group.name);
});
} else {
g_form.addOption('requested_support_groups', '', '-- No groups found --');
}
} catch (e) {
g_form.addErrorMessage('Error loading groups: ' + e.message);
}
});
}

 

Script include

var GroupManagerUtils = Class.create();
GroupManagerUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroupsByUnitManagers: function() {

// Get selected unit managers (comma-separated sys_ids)
var managerList = this.getParameter('sysparm_managers');
if (!managerList) return '[]';

var groups = [];
var gr = new GlideRecord('sys_user_group');

// Query groups where selected users are unit managers
gr.addQuery('u_unit_manager', 'IN', managerList); 
gr.query();

while (gr.next()) {
groups.push({
sys_id: gr.getValue('sys_id'),
name: gr.getDisplayValue('name')
});
}
return JSON.stringify(groups);
},
type: 'GroupManagerUtils'
});

 

Thank you

Omat

8 REPLIES 8

Aniket Chavan
Tera Sage
Tera Sage

Hello @omatD ,

I just checked your script and spotted a couple of small issues — for example, in the client script, unit_managers is misspelled as unit_manages. Also made a few other minor tweaks. So Give it a try to the script below and let me know how it works for you.


Updated Client Script (onChange):

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) return;

    // Get selected users from the List Collector (comma-separated sys_ids)
    var unitManagers = g_form.getValue('unit_managers');

    if (!unitManagers) {
        g_form.clearOptions('requested_support_group');
        return;
    }

    var ga = new GlideAjax('GroupManagerUtils');
    ga.addParam('sysparm_name', 'getGroupsByUnitManagers');
    ga.addParam('sysparm_managers', unitManagers);
    ga.getXMLAnswer(function(response) {
        try {
            var groups = JSON.parse(response);
            g_form.clearOptions('requested_support_group');

            if (groups.length > 0) {
                groups.forEach(function(group) {
                    g_form.addOption('requested_support_group', group.sys_id, group.name);
                });
            } else {
                g_form.addOption('requested_support_group', '', '-- No groups found --');
            }
        } catch (e) {
            g_form.addErrorMessage('Error loading groups: ' + e.message);
        }
    });
}


Updated Script Include:

var GroupManagerUtils = Class.create();
GroupManagerUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getGroupsByUnitManagers: function() {
        var managerList = this.getParameter('sysparm_managers');
        if (!managerList) return '[]';

        var groups = [];
        var gr = new GlideRecord('sys_user_group');

        // Use encoded query to support multi-user reference (comma-separated match)
        gr.addEncodedQuery('u_unit_managersIN' + managerList);
        gr.query();

        while (gr.next()) {
            groups.push({
                sys_id: gr.getValue('sys_id'),
                name: gr.getDisplayValue('name')
            });
        }

        return JSON.stringify(groups);
    },
    type: 'GroupManagerUtils'
});

 

Please mark Correct if this solves your query, and 👍 Helpful if you found the response valuable.

 

Best regards,
Aniket Chavan
🏆 ServiceNow MVP 2025 | 🌟 ServiceNow Rising Star 2024

 

@Aniket ChavanSince 'Group' is a reference field based on his requirement, will addOptions still work for it?

Thank you @Aniket Chavan. I tried these updates but the requested support group shows all the groups. 

@omatD 

Did you get a chance to check my below response?

It will work for you.

You should use advanced ref qualifier and not client script

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