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.

How to add users from watch list to the CC of email notification

Rutusha
Tera Contributor

Hi all,

I have a requirement where i need to add all the watchlist users to the CC of email notification.

I have written the mail script but not getting the outcome.

Could someone help me on this.

Tried putting logs.

Log inside while loop is not coming.

 

function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
if(!current.watch_list.nil()){
var watcherIds = current.watch_list.toString().split(',');
gs.info("watcherIds-->"+watcherIds);
var user = new GlideRecord("sys_user");
user.addQuery('sys_id','IN',watcherIds);
user.addQuery('email','!=','');
user.query();
gs.info('test is' +"sys_idIN" +current.watch_list+"");
while(user.next()){
gs.info('user in the watchlist are' +watcherIds);
email.addAddress('cc', user.email, user.displayName());
}
}
})(current, template,email, email_action, event);
 
@Ankur Bawiskar Could you please help me here
10 REPLIES 10

SoniaShridhar13
Giga Guru

@Rutusha  Hi!  Use below-

current.watch_list +=email.copied;

or

current.watch_list = current.watch_list  + ',' +email.copied;

 

Please mark it helpful if it helps.

 

Thanks,

Sonia

HI @SoniaShridhar13  - No luk  still not working 

Hi @SoniaShridhar13,

 

That is the other way around, to add users in cc to the watchlist.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Peter Bodelier
Giga Sage

Hi @Rutusha,

 

Use this script in your email script:

 

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());}}

From: Example scripting for email notifications (servicenow.com)

 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.