how to Add Multiple Recipients to CC via email notification Script

Raunak kapoor2
Tera Expert

I am trying to create a email notification script and add multiple users in CC.

I need help with the script to add multiple user in CC 

2 ACCEPTED SOLUTIONS

ChrisBurks
Mega Sage

According to ServiceNow's documentation the basic script to add a single CC recipient is as the following:

 email.addAddress("cc", <email address goes here >, <recipient's name goes here>)

Resource: here

or here

 

The method seems like it's only built to add one at a time thus depending upon how you are getting your list of recipients/users the script would need to have a looping against your list.

From a GlideRecord query script you can use the usual while loop as in the below example taken from the first resource link above:

if(!current.watch_list.nil()){
   //get watch list addresses and add to cc
   var watcherIds = current.watch_list.split(",");
 
   //get user records
   var user = new GlideRecord("sys_user");
   user.addQuery("sys_id", watcherIds);
   user.addQuery("notification",2); 
   //email
   user.addQuery("email","!=","");
   user.query();
 
   while(user.next()){
      //add to cc list    
      email.addAddress("cc", user.email, user.getDisplayValue());
}}

 

In the above example in the "while" loop, each time it comes across a user record found that meets the query it add that user to the "cc" for the notification. The script also is showing what is needed when you have a list of user record sys_ids. The watch list  field returns a string list of sys_ids separated by a comma when getting it's value. The script above then turns that list into an array by using the .split(",") method and passes it to the GlideRecord query script.

 

But to really pin point what kind of scripting you need, it would help if you could provide details on how you are getting the multiple users. Is it possible to post any scripting that you have done thus far?

 

 

View solution in original post

3 REPLIES 3

ChrisBurks
Mega Sage

According to ServiceNow's documentation the basic script to add a single CC recipient is as the following:

 email.addAddress("cc", <email address goes here >, <recipient's name goes here>)

Resource: here

or here

 

The method seems like it's only built to add one at a time thus depending upon how you are getting your list of recipients/users the script would need to have a looping against your list.

From a GlideRecord query script you can use the usual while loop as in the below example taken from the first resource link above:

if(!current.watch_list.nil()){
   //get watch list addresses and add to cc
   var watcherIds = current.watch_list.split(",");
 
   //get user records
   var user = new GlideRecord("sys_user");
   user.addQuery("sys_id", watcherIds);
   user.addQuery("notification",2); 
   //email
   user.addQuery("email","!=","");
   user.query();
 
   while(user.next()){
      //add to cc list    
      email.addAddress("cc", user.email, user.getDisplayValue());
}}

 

In the above example in the "while" loop, each time it comes across a user record found that meets the query it add that user to the "cc" for the notification. The script also is showing what is needed when you have a list of user record sys_ids. The watch list  field returns a string list of sys_ids separated by a comma when getting it's value. The script above then turns that list into an array by using the .split(",") method and passes it to the GlideRecord query script.

 

But to really pin point what kind of scripting you need, it would help if you could provide details on how you are getting the multiple users. Is it possible to post any scripting that you have done thus far?

 

 

Its working with  "

 email.addAddress("cc", user.email.toString(), user.getDisplayValue());