Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Script to return count of group members in a group

Priyanka Chaud1
Tera Contributor

I have a requirement where we have a catalog item variable field in that we need to populate the count of group members present in the group.

Basically its having a 2000 - number of members present in group count to show in that field

Can anyone help me with that requirement

1 ACCEPTED SOLUTION

@Priyanka Chaud1 

I already shared the approach simply subtract

javascript: var count = 0;
var ga = new GlideAggregate('sys_user_grmember');
ga.addQuery('group', 'groupSysId');
ga.addAggregate('COUNT');
ga.query();
if (ga.next()) {
    count = parseInt(ga.getAggregate('COUNT'));
}
count = 2000-count;
count;

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

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Priyanka Chaud1 

so user will select group in 1 variable and then other variable is string where you want to store count

You can use onChange catalog client script with GlideAjax

Use GlideAggregate like this and return the count

I hope you know how to use GlideAjax

var GetGroupMemberCount = Class.create();
GetGroupMemberCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCount: function() {
        var groupId = this.getParameter('sysparm_group_id');
        var ga = new GlideAggregate('sys_user_grmember');
        ga.addQuery('group', groupId);
        ga.addAggregate('COUNT');
        ga.query();
        if (ga.next()) {
            var count = parseInt(ga.getAggregate('COUNT'));
            return count.toString();
        }
        return '0';
    }
});

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

Hey @Ankur Bawiskar thanks for the reply the group is just one group that will be used there is no variable for group selection

@Priyanka Chaud1 

then in that string variable use this default value

javascript: var count = 0;
var ga = new GlideAggregate('sys_user_grmember');
ga.addQuery('group', 'groupSysId');
ga.addAggregate('COUNT');
ga.query();
if (ga.next()) {
    count = parseInt(ga.getAggregate('COUNT'));
}
count;

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

anshul_goyal
Kilo Sage

Hello @Priyanka Chaud1 

Just to confirm, you're adding the assignment group name to a text/string field, and that group contains 2,000 members, is that correct?

Thanks