Mail Script - CC the email address from Watch List

Jon S_
Giga Contributor

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

    }

}

1 ACCEPTED SOLUTION

martinb
Giga Contributor

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;


}


View solution in original post

12 REPLIES 12

justin_drysdale
Mega Guru

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


                                }


                }


    }


}    


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?


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.

Strange... this one doesn't work for me. It won't even include the regular users let alone any email addresses.