- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2017 11:12 AM
So I've implemented the following mail script for Users from the Watch List to be cc'd and it works great. Problem is, we also want to include email addresses added manually to the Watch List (e.g. a Distribution List email address) but I'm hitting a wall on how to write this in or if it needs to be a different mail script altogether. Basically, CC everything from the Watch List and not just Users in the system but also email addresses. Any help would be appreciated.
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());
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-01-2017 12:32 PM
This works for me:
var watcherIds = current.watch_list.split(',');
for (var i=0;i<watcherIds.length;i++) {
if (watcherIds[i].indexOf('@') > -1) {
email.addAddress("cc", watcherIds[i], watcherIds[i]);
} else {
var userVals = getUserEmail(watcherIds[i]);
if (userVals.length > -1) {
email.addAddress("cc",userVals[0],userVals[1]);
}
}
}
function getUserEmail(inId) {
var vals = [];
var user = new GlideRecord("sys_user");
user.addQuery("sys_id",inId);
user.addQuery("notification", '2'); //notifications enabled
user.addQuery("email", "!=", "");
user.query();
if (user.next()) {
vals.push(user.getValue('email'));
vals.push(user.getValue('user_name'));
}
return vals;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2017 02:07 PM
This code doesn't pickup manually added addresses? Hmm, I think you'd have to loop over each value in the watchIds array like this (untested):
if (!current.watch_list.nil()) {
//get watch list addresses and add to cc
var watcherIds = current.watch_list.split(",");
var len = watcherIds.length;
for(var i = 0; i!=len; i+=1) {
if(watcherIds[i].toString().indexOf("@") > -1) {
//regular manual email address with an @ symbol:
email.addAddress("cc", watcerIds[i], watcherIds[i]);
}else{
//service now user sys_id:
var user = new GlideRecord("sys_user");
user.addQuery("sys_id", watcherIds[i]);
user.addQuery("notification", 2); //email
user.addQuery("email", "!=", "");
user.query();
while (user.next()) {
//add to cc list
email.addAddress("cc", user.email, user.getDisplayValue());
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-01-2017 09:28 AM
Unfortunately, it does not. And I'm surprised that in all my research and how many people are using this that nobody else has either noticed or reported it (or needed it). I tried your code, fixing watcerIds[i] but no luck. Any ideas?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-01-2017 09:53 AM
This is what I do. Because there could be email addresses which are not users.
if (current.watch_list!='')
{
var watchlist = current.watch_list.split(',');
for (var i=0;i<watchlist.length;i++)
{
var iuser = new GlideRecord('sys_user');
iuser.addQuery('sys_id',watchlist[i].toString());
iuser.query();
if (iuser.next())
email.addAddress("cc", iuser.email.toString());
else
email.addAddress("cc", watchlist[i]);
}
}
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-01-2017 10:24 AM
Strange... this one doesn't work for me. It won't even include the regular users let alone any email addresses.