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

Tried the same way earlier as well but still not working

if(!current.watch_list.nil()){
 var watcherIds = current.watch_list.split(',');
gs.info("watcherIds-->"+watcherIds);
var user = new GlideRecord("sys_user");
user.addQuery('sys_id',watcherIds);
user.addQuery('email','!=','');
user.addQuery("notification",2); 
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.getDisplayValue());

You are getting the log lines? 
Do you only have users, or also emailaddresses in the watch list?


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

I am not getting the log inside while loop apart from that all other logs are printing.

I have only users in the watchlist and not the email ids

Just tried it in my instance. Script works as it should. 

Please try the query you are running manually, to see if the users can indeed be found.


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

Ankur Bawiskar
Tera Patron
Tera Patron

@Rutusha 

what debugging have you done so far?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader