gliderecord on group table

shid077
Tera Contributor

Please provide me the query or code to print the group member name and count of group name "ACME Support" which have 4 group members.

7 REPLIES 7

psphanindrakuma
Tera Guru

@shid077 

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group.name','ACME Support');
gr.query();
gs.log(gr.getRowCount())
while(gr.next()){
gs.log(gr.user.name)
}

Please mark my answer as correct and helpful if it help you

 

Thanks,

Phanindra.

 

 

 

 

Charan123
Tera Expert

Hi @shid077 ,
Please use below code..

var gr= new GlideRecord('sys_user_grmember');
gr.addQuery('group.name','ACME Support');
gr.query();
while(gr.next()){
gs.info(gr.user.user_name);
}

Please mark my answer as correct and helpful if it help you

Thanks,

Ashish Parab
Mega Sage
Mega Sage

@shid077 

 

To retrieve group members' names in ServiceNow, you can use a GlideUser API. Below is an example of JavaScript code that you can use in a ServiceNow script:

// Assuming 'groupSysID' is the sys_id of the group you want to retrieve members for
var groupSysID = 'YOUR_GROUP_SYS_ID';

var group = new GlideGroup(groupSysID);
var members = group.getMembers();

while (members.next()) {
var user = new GlideUser(members.user);
var userName = user.getDisplayName();
gs.print(userName);
}

Please replace 'YOUR_GROUP_SYS_ID' with the actual sys_id of the group you're interested in.




you can use a GlideAggregate query to count the number of groups with the name "ACME Support" and having 4 group members. Here's an example of the query:

var ga = new GlideAggregate('sys_user_grmember');
ga.addAggregate('COUNT', 'group');
ga.addEncodedQuery('group.name=ACME Support^group.member_count=4');
ga.query();

var count = 0;
if (ga.next()) {
count = parseInt(ga.getAggregate('COUNT', 'group'));
}

gs.print("Number of groups named 'ACME Support' with 4 members: " + count);



1. It creates a new instance of GlideAggregate on the sys_user_grmember table, which is where group membership information is stored.

2. It adds an aggregate function to count the number of unique groups ('COUNT', 'group').

3. It adds an encoded query to filter groups with the name "ACME Support" and having exactly 4 members.

4. It executes the query.

5. It retrieves and prints the count.

Please mark helpful or correct, if my answer is applicable for your case.
Thanks & Regards,
Ashish Parab