How to filter "Assigned To" users based on Assignment Group hierarchy (including child and grandchil

Nivetha23jay
Tera Contributor
  • I want the "Assigned To" field to display only those users who are members of the selected Assignment Group, its child groups, and its grandchild groups. This means: If a group is selected in the Assignment Group field, The Assigned To field should only list users who belong to that group, As well as users from any child or grandchild groups related to it. How can this be achieved using a reference qualifier or script include in ServiceNow?
2 REPLIES 2

pavani_paluri
Tera Guru
Tera Guru

Hi @Nivetha23jay ,

 

You can do it using Script include and advanced reference qualifier

 

Script include make it client callable:

var GetGroupMembersRecursive = Class.create();
GetGroupMembersRecursive.prototype = {
initialize: function() {},

getUsersFromGroupAndSubgroups: function(groupId) {
var groupIds = [groupId];
this._getChildGroups(groupId, groupIds, 2); // limit to 2 levels (children + grandchildren)

var userSet = new Set();
var m2m = new GlideRecord('sys_user_grmember');
m2m.addQuery('group', 'IN', groupIds);
m2m.query();
while (m2m.next()) {
userSet.add(m2m.user.toString());
}

return Array.from(userSet); // return as array of sys_ids
},

_getChildGroups: function(parentId, collector, depth) {
if (depth <= 0) return;

var gr = new GlideRecord('sys_user_group');
gr.addQuery('parent', parentId);
gr.query();
while (gr.next()) {
collector.push(gr.sys_id.toString());
this._getChildGroups(gr.sys_id.toString(), collector, depth - 1);
}
},

type: 'GetGroupMembersRecursive'
};

 

Give this in advanced reference qualifier on assigned to field:

javascript&colon;GetGroupMembersRecursive.getUsersFromGroupAndSubgroups(current.assignment_group);

 

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Pavani P

 

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Nivetha23jay 

 

It is out-of-the-box (OOTB) if I'm not wrong.

Just make sure that the group has a parent assigned — the rest should work as expected with OOTB functionality.

 

DrAtulGLNG_0-1753902481603.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************