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

There appears to be a spelling error in line 9 of the code. Should be:


                  email.addAddress("cc", watcherIds[i], watcherIds[i]);


Yea... I caught that but still no luck.


It doesnt matter if you dont pass the display values in addAddress.



Please mark this response as correct or helpful if it assisted you with your question.

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;


}


rahul0602
Kilo Contributor

Hi Jon

 

What does current here refer to?

Are you calling this mail script from any BR or script Inc?

I too have a similar requirement but have no clue on how to send the parameters to the notification.