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

No 2000 is a default value we need to subract from the the count of members of assignment group and show it in one variable @anshul_goyal so its 2000- count of member of group = variable field value

@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

@Ankur Bawiskar  do i need to do Glideajax and write a client script or directly put in default value of field?

@Priyanka Chaud1 

nothing, simply put that in default value of that string variable

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