Getting members names and emails of a group via script

keshab_saha
Kilo Contributor

How I can get member names and their emails of a specific group via script ?

5 REPLIES 5

Mikolz
Kilo Contributor

Do you need to get this information via a script? You could set up a report on the user role table and set your needed filters and get the information that way.


yes, I need this via script. Requirement is something like - moment I will select the assignment group, based on selective company, group members of that assignment group will automatically be added as watch list member.


this can be done via the sys_user_grmember_list table. Based on the group you can query all the group members and put them in the watchlist. put the members in a new array and join them with the watchlist.


limedzeze
Giga Contributor

I had this same question, and couldn't really find the answer anywhere, but the mention above of the sys_user_grmember table (we don't have a sys_user_grmember_list table) led me down a path of discovery and I was able to come up with a script to do just this. We needed to do this in case there was no DL associated with a group. Here is what I came up with:



<mail_script>
if (!current.group_list.nil()) {
//get group list addresses and add to cc
var groupers = current.group_list.split(",");
var gl = groupers.length;

for (var j = 0; j < gl; j++) {


//get guser records
var guser = new GlideRecord("sys_user_group");
guser.addQuery("sys_id", groupers[j]);
guser.query();

while (guser.next()) {
if (guser.email != "") {
//add to cc list
email.addAddress("cc", guser.email, guser.getDisplayValue());
//email.addAddress("bcc", guser.email, guser.getDisplayValue());
} else {
var gmembers = new GlideRecord("sys_user_grmember");
gmembers.addQuery("group.sys_id", groupers[j]);
gmembers.addQuery("user.notification", 2); //email
gmembers.addQuery("user.email", "!=", "");
gmembers.query();

while (gmembers.next()) {
email.addAddress("cc",gmembers.user.email, gmembers.user.getDisplayValue());
}
}
}

}

}
</mail_script>