How to add mulitple groups to Notification (email) depending on user selections

Mike Cumbee
Giga Expert

In a Change Request form, users can select which department groups should review the proposed changes.   This can be up to 11 groups.  

Since many of the group members are in several of these groups, I need to add each of the members of the selected groups so they only get one email from this notification.   I already have the id of all the groups.   Can I use an array for the member's email and add this array to in the Notification script (Advanced)?   If so, what is the field I will set in Notification?   What is the best way to get the user's email via the group members?

Thanks in advance!

Mike

1 ACCEPTED SOLUTION

Hi Mike,



Let's make an array of objects. I've also put the values in the bold line for you.



if(current.u_it_services == 'Yes'){


      getMembers('25c64ce70d431200083ba4f662762aab');


}



var uList = getMembers(groupID);



gs.log("userList: "+userList);



function getMembers(groupID){


        var userList = [];


      var mem = new GlideRecord('sys_user_grmember');


      mem.addQuery('group', groupID);


      mem.query();



      while (mem.next()){


                  var uObj = {};


                  uObj.id = mem.getValue('user');


                  uObj.name = mem.user.getDisplayValue();


                  userList.push(uObj);


              //email.addAddress("cc", mem.getValue('user'), mem.user.getDisplayValue());


      }


        return userList;


}


View solution in original post

11 REPLIES 11

Chuck Tomasi
Tera Patron

Hi Mike,



If the group has a distribution list, you can just add the field that contains all the groups in the Users/Groups in fields (below):



find_real_file.png



If not, you can go through the sys_user_grmember table and query for the group members. Add the members to the Cc list with the



email.addAddress("cc", user address, user name);



Put that in a loop of all your recipients, and you're good to go.



Don't worry about de-duplicating, the mail engine is smart enough to catch that.



Reference: Scripting for Email Notifications - ServiceNow Wiki


Chuck;



Thank you for the quick response.   Below is 's a capture of the possible list of groups.   As said before, they contain multiple members and some of those members are in multiple groups.



Is it possible to add the group members to the "To" line in the email?



Good to hear about the De-duplicating, I was going to loop through an array for each member and only add to an array if not present.



So if I have the groupID (like: 14ad6dcbf8e921aa083cf728e5184e01) - I can access it's members in a loop and I will have their userID, from which I can gain the user name?



Thx!



find_real_file.png


Hi Mike,



You can query table "sys_user_grmemeber" by passing "group sys_id/group.name" as parameter and fetch users who belongs to that group.


Hi Mike,



Thanks for the screenshot. I thought the members were values in the record somewhere. This gets interesting if you only have yes/no values on the field.





Short answer to your question above, yes, if you have the sys_id of a group, you can get the members like this:



var groupID = '14ad6dcbf8e921aa083cf728e5184e01';


var userLIst = [];



var mem = new GlideRecord('sys_user_grmember');


mem.addQuery('group', groupID);


mem.query();



while (mem.next()) {


        userList.push(mem.getValue('user'));


}



// You now have an array of users.



As for adding them to the To line, I haven't found a supported way to do that yet. Often asked.