How to show only users from respective group in reference field (sys_user) table

Santhosh23
Tera Expert

Can anyone please explain me how to filter the users from only one group in reference fields reference to sys_user table.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you need to use advanced reference qualifier on that user reference field

javascript: 'sys_idIN' + new GetGroupMembers().getUsers();

Script include

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

	getUsers: function(){

		var arr = [];
		var gr = new GlideRecord("sys_user_grmember");
		gr.addQuery("group.name", "ABC Group");
		gr.query();
		while(gr.next()) {
			arr.push(gr.getValue('user'));
		}
		return arr.toString();

	},

	type: 'GetGroupMembers'
};

Regards
Ankur

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

View solution in original post

9 REPLIES 9

Can this same script include be used for referencing multiple groups instead of just one?? If so, how??

 

@Brandon R1 Yes we can use it for multiple group.

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

	getUsers: function(){
var groupIDs = ['sys_id1','sys_id2','sys_id3'];  // save comma separated list of groups sys_id  
		var arr = [];
		var gr = new GlideRecord("sys_user_grmember");
		gr.addQuery("group.sys_id","IN", groupIDs.toString());  // and small change to this line 
		gr.query();
		while(gr.next()) {
			arr.push(gr.getValue('user'));
		}
		return arr.toString();
	},

	type: 'GetGroupMembers'
};

 

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

thanks, it worked

Tim3
Tera Contributor

Using this script include method now requires users in the portal to have a license to view the results.
See KB0720034 
This changed in June 2021.
If you require users without a license to view the results, you will need to edit the ACL, or re write the query on the variable

RudhraKAM
Tera Guru

This might be Old post but just adding other option, so that some one can use it , 

 

In the Reference variable , Select group member table and use the group sysID , this will fix the issue 

RudhraKAM_0-1689942149935.png