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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-05-2018 06:08 AM
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
- Labels:
-
Personal Developer Instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-05-2018 06:17 AM
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
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-27-2024 06:15 AM
var numbeOfMembers = gr.getRowCount();
I'm getting function error for this

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-05-2018 06:17 AM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-26-2019 09:20 AM
What if you want to include groups with no members? is that possible?