How to add RITM watch list users to email CC using a mail script?

hrafael
Tera Contributor

I’m trying to automatically add all users from the RITM watch list to the CC field of an outbound email in ServiceNow, but they are not being added.

I created the following mail script and referenced it in my email template with ${mail_script:setCopyEmail}. The notification is triggered correctly and the main recipient receives the email, but no addresses appear in CC.

Mail script

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

	var mailList = current.watch_list || "";

	if (!mailList) {
		return;
	}

	var gr = new GlideRecord("sys_user");
	gr.addEncodedQuery("sys_idIN", mailList);
	gr.query();

	var added = {};

	while (gr.next()) {
		var userMail = gr.getValue("email");
		if (userMail && !added[userMail]) {
			email.addAddress("cc", userMail);
			added[userMail] = true;
		}
	}

})(current, template, email, email_action, event);

 

Email template:

${mail_script:generate_subject_detail}
${mail_script:show_close_notes_if_any}
${mail_script:setCopyEmail}
Request number: ${number}
Opening date: ${mail_script:formatted_opening_date}
Requester: ${mail_script:get_requester_dynamically}

Request summary:
${short_description}
${description}
${mail_script:detailed_request_information_en}
${mail_script:restart_computer_it_en}

 

Could someone point out what I might be missing, either in the mail script or in the way I’m calling it from the email template?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@hrafael 

update as this

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var mailList = current.watch_list.toString() || "";

    if (!mailList) {
        return;
    }

    var gr = new GlideRecord("sys_user");
    gr.addActiveQuery();
    gr.addEncodedQuery("sys_idIN" + mailList);
    gr.query();
    while (gr.next()) {
        var userMail = gr.getValue("email");
        if (userMail) {
            email.addAddress("cc", userMail, gr.getDisplayValue());
        }
    }

})(current, template, email, email_action, event);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron

@hrafael 

update as this

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var mailList = current.watch_list.toString() || "";

    if (!mailList) {
        return;
    }

    var gr = new GlideRecord("sys_user");
    gr.addActiveQuery();
    gr.addEncodedQuery("sys_idIN" + mailList);
    gr.query();
    while (gr.next()) {
        var userMail = gr.getValue("email");
        if (userMail) {
            email.addAddress("cc", userMail, gr.getDisplayValue());
        }
    }

})(current, template, email, email_action, event);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

suraj sengar
Tera Guru

@hrafael  - 
Try this code 

(function runMailScript(current, template, email, email_action, event) {

 

    if (!current.watch_list) {
        return;
    }

 

    var users = current.watch_list.split(",");
    var added = {};

 

    for (var i = 0; i < users.length; i++) {
        var userGR = new GlideRecord("sys_user");
        if (userGR.get(users[i])) {
            var mail = userGR.getValue("email");
            if (mail && !added[mail]) {
                email.addAddress("cc", mail);
                added[mail] = true;
            }
        }
    }

 

})(current, template, email, email_action, event);


OR 

Root Cause (Why CC is Empty)
In ServiceNow:

Mail scripts referenced inside the email body/template cannot reliably modify email headers (To/CC/BCC).

By the time ${mail_script:setCopyEmail} is executed:

The email headers (To/CC/BCC) are already finalized
email.addAddress("cc", …) is silently ignored

That’s why:

Notification triggers
Main recipient receives the email
CC is empty

This is expected platform behavior, not a bug in your script.

Correct Ways to Add Watch List Users to CC
Option 1 (Best Practice): Use the Notification “CC” field
If this is an RITM (sc_req_item):

Open the Notification
In the Who will receive section
Set CC to:
Watch list


ServiceNow automatically resolves watch_list → user emails
No script needed
Supported by platform
Most reliable

Tanushree Maiti
Kilo Patron

Hi @hrafael 

 

Try this ,

Mail script
 
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
 
    if(!current.watch_list.nil()){
var mailList = current.watch_list.split(',');
for (var i=0; i< mailList.length; i++) {
var grUser = new GlideRecord("sys_user");
grUser.addQuery("sys_id", mailList[i]);
grUser.addQuery("notification",2);
grUser.addQuery("email","!=","");
grUser.query();
 
if (grUser.next()){
email.addAddress('cc', grUser.email, grUser.getDisplayValue());
}
else{
email.addAddress('cc', mailList[i]);
}
}
}
})(current, template, email, email_action, event);
Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: