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.

Get all users from group

kiranped
Giga Contributor

Hi All,

Is there any js-function to get the user list object by group-name or is there any script to get all users from a group?

example group name is: IS&T Senior Leadership Team

Thanks & Regards,

Kiran Pedduri

1 ACCEPTED SOLUTION

Hi Kiran,



Sorry, I had an error in the first one. I was pushing the sys_id of the membership record, not the users. I've corrected it.



Easy change... You can 'dot-walk' to get that. If you want the user_name field (labeled User ID, like chuck.tomasi) you can change the answer.push() line to this



answer.push(mem.user.name.toString()).



You want the email,answer.push(mem.user.email.toString());


View solution in original post

22 REPLIES 22

OR try this,



function getMemberSysIds(groupName) {


var answer = [];


var mem = new GlideRecord('sys_user_grmember');


mem.addQuery('group.name', 'IS-HOSTING-SSOIN-WINTEL-DC-Global');


mem.query();


while (mem.next())


answer.push(mem.user.email.toString());


gs.print(answer);


}


HI Shishir,


It is showing empty when i execute


I think in this line i removed ".name"---------------- mem.addQuery('group.name', 'IS-HOSTING-SSOIN-WINTEL-DC-Global');


i replaced to mem.addQuery('group', 'IS-HOSTING-SSOIN-WINTEL-DC-Global');


because in "sys_user_grmember" the field name is only "group"


please correct me if i am wrong. infact i tried your script but also it is showing empty after execution


find_real_file.png


No, please use with name, group field is not a string field on sys_user_grmember table, it is actually referenced to sys_user_group table.



mem.addQuery('group.name', 'IS-HOSTING-SSOIN-WINTEL-DC-Global');


again it is showing same. i am getting empty reply


find_real_file.png


In background script, you do not need to wrap under the function, please try below it will work in background script.



var answer = [];


var mem = new GlideRecord('sys_user_grmember');


mem.addQuery('group.name', 'IS-HOSTING-SSOIN-WINTEL-DC-Global');


mem.query();


while (mem.next())


answer.push(mem.user.email.toString());


gs.print(answer);