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;



This is working, in that I am able to detect if the user selected 'Yes' for a department AND your script fetches the USER_ID and pushes it into the array.



I'm going to bypass using the array since we can use email.addAddress() to the CC line.



What is the script for fetching the Name of the user (Mike Cumbee) and the user email (mcumbee@this.com)?




Thank you for your assistance!


Mike




//////////////////////////////////////////////////       CURRENT SCRIPT:


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


      getMembers('25c64ce70d431200083ba4f662762aab');


}



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



function getMembers(groupID){      


      var mem = new GlideRecord('sys_user_grmember');


      mem.addQuery('group', groupID);


      mem.query();



      while (mem.next()){


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


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


      }


}


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;


}


Chuck;



I've implemented part of the script provided - it failed and I recognize part of the problem.



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



It needs the <mail script> tags (of which I am unfamiliar with and have not found on the wiki), can you provide that?



Also, I know that I can get the current user's record with:



                                  gs.getUser();



What is the preferred method to get the record of a user NOT logged in?



Thanks!


Mike


Hi Mike,



The modern way of doing email scripting is done like this:



${mail_script:my_mail_script_name}



Then in a separate (reusable) mail script file called my_mail_script_name, you have the code.



Events and Email Notification - ServiceNow Wiki



To get the record of a user not currently logged in, you need to do a GlideRecord query on sys_user.



GlideRecord - ServiceNow Wiki


Working!!!!   My thanks to all in this endeavor.




Below is the final version of the Get User Email with Group_ID:




// Get User's email using Group ID



var userList = [];


var emails = '';


var email = new GlideRecord('sys_email');



//check if IT_Services was selected:


if(current.u_it_services == 'Yes')


{


      getMembers('~');


}



//   Multiple other groups....



//   Having gone through all of the Coordination Groups and added them to the userList array,


//   check it and add the to the emails string.


if(userList.length >0)


{


      for(var i = 0; i< userList.length; i++)


      {


              if(emails != '')


              {    


                      emails += (", "+userList[i]);


              }


              else


              {


                      emails = userList[i];


              }


      }


   


      email.reply_to = emails;


}




function getMembers(groupID)


{    


      var mem = new GlideRecord('sys_user_grmember');


      mem.addQuery('group', groupID);


      mem.query();


   


      //for each member of the group


      while (mem.next()) {


           


              // see if this email has been already added:


              var found = false;


              for(var e = 0; e<userList.length; e++)


              {


                      if(mem.user.email == userList[e])


                      {


                              found = true;


                      }


              }


           


              //if not, add to the userList


              if(found ==false)


              {


                      userList.push(mem.user.email);


              }


      }


}