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

Jon S_
Giga Contributor

Current. is referring to the incident table, the table that the notification is based on. The mail script is embedded in the html of the notification. This might help: https://docs.servicenow.com/bundle/jakarta-servicenow-platform/page/script/server-scripting/concept/c_ScriptingForEmailNotifications.html

 

rahul0602
Kilo Contributor

Hi Jon

 

Thanks! It helped me taking a step forward.

I have written a code in the email script and calling from the notification. It is working fine but in CC it's going only to one person/DL despite adding three in that list .

var inputStr = current.u_audience_list;
var adList = inputStr.split(',');
// var userEmail='';
var list1 = [];
for (var i = 0; i < adList.length; i++)
{ var user = new GlideRecord("u_distribution_list");
user.addQuery("sys_id", adList[i]);
//user.addQuery("email", "!=", "");
user.query();
while (user.next()) {

list1.push(user.u_email);
}
if(list1!='')
{ gs.log('check five'+list1,'one');
email.addAddress("cc",list1, list1.getDisplayValue());
}}

in the log it's showing all five added in the list, but not on the cc of the email.

Can you please help me on this.

Jon S_
Giga Contributor

Try this out if you are doing this for Incidents:

if (!current.watch_list.nil()) {
	// CC recipients
	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;
}