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

Hi @Ankur Bawiskar,  Thank you for your response. Do you want me to use the reference qualifier and not the client script/script include?

 

I tried it but got the same error as shown in the attachment

@omatD

yes please deactivate the client script and only keep reference qualifier

Also use colon : instead of : in your advanced ref qualifier

Somehow ServiceNow community doesn't treat that character well

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

Ankur Bawiskar
Tera Patron
Tera Patron

@omatD 

you should use advanced ref qualifier on 2nd variable and show only those users.

onChange client script is used to set some value and not to apply reference qualifier

something like this will work

javascript:'u_unit_managerIN' + current.variables.unit_manages.toString();

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

@omatD 

Hope you are doing good.

Did my reply answer your question?

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