Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to count number of group members in the group? for example "sys_user_group" is the table for group in serviceNow and CABApproval group contain 5 members how can i achieve this with coding

shahebaz1
Kilo Contributor

How to count number of group members in the group? for example "sys_user_group" is the table for group in serviceNow and CABApproval group contain 5 members  how can i achieve this with coding

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shahebaz,

Group membership is stored in sys_user_grmember table

Query this table with group name and get the rowCount. you will know the number of members

var gr = new GlideRecord('sys_user_grmember');

gr.addQuery('group.name', '<nameOfGroup>');

gr.query();

var numbeOfMembers = gr.getRowCount();

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

var numbeOfMembers = gr.getRowCount(); 

I'm getting function error for this 

 

Chuck Tomasi
Tera Patron

I recommend using GlideAggregate if you all you are going to do is count. Group membership is a many-to-many table called sys_user_grmember.

You could something like this:

function countMembers(groupName) {
	var mem = new GlideAggregate('sys_user_grmember');
	mem.addQuery('group.name', groupName);
	mem.addAggregate('COUNT');
	mem.query();

	var count = -1; // default to error state since 0 is a valid count

	if (mem.next()) {
		count = mem.getAggregate('COUNT');
	}

	return count;
}

What if you want to include groups with no members?  is that possible?