Set added external users/users in watchlist field to cc in email notification

Rakesh50
Mega Sage

Hello,

 

We have a custom field users and also we have new email address. If we enter any external email address and if we select any users in watch list need to send notification and add these users as ''CC'' in email notification.

I have written below mail script and not working. I have added screenshots please check and give suggestions

 var getListusers = current.u_users.split(","); // Replace "watch_list" with your field name
email.addAddress("cc", getListusers, getListusers.getDisplayValue());

Rakesh50_2-1684506873415.png

Rakesh50_3-1684506931224.png

 

 

1 ACCEPTED SOLUTION

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

Hey @Rakesh50, here's a full blown script block you can insert inside of your mails script to add both users, as well as external email address to the CC field. I have broken it up to sections and added comments so you can follow what it does,, and change it to your needs as required.

I have tested and it's working as expected (make sure the list field name, e.g. u_users is correct!).

 

 

try {

		/* Create an array from the user list values */
        var getListusers = current.u_users.split(',');


        /* Create a separate array with email addresses using RegEx */
        var r = new RegExp('([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)', 'jgi');
        var emailArr = getListusers.filter(function(x) {
            return x.match(r) ? true : false;
        });


        /* Reduce the original array to users only */
        var arrayUtil = new ArrayUtil();
        var userArr = arrayUtil.diff(getListusers, emailArr);


        /* Add users to CC */
        var userGr = new GlideRecord('sys_user');
        userGr.addEncodedQuery('sys_idIN' + userArr + '^notification=2^emailISNOTEMPTY');
        userGr.query();
        while (userGr.next()) {
            email.addAddress('cc', userGr.email, userGr.getDisplayValue());
        }

		/* Add email addresses to CC */
		var emailAddress = '';
		for(i = 0; i < emailArr.length; i++) {
			emailAddress = emailArr[i].toString();
			email.addAddress('cc', emailAddress);
		}


} catch(err) {
		gs.log('users_to_cc mail script failed: ' + err);
}

 

 

 

Here are some screenshots too.

 

  1. Calling the mail script from the notification records body (inn may case, the mail script is called 'users_to_cc', make sure you use the correct name):
    Screenshot 2023-05-22 at 17.31.28.png 
  2. Populated CC field in the email record:
    Screenshot 2023-05-22 at 17.39.37.png

View solution in original post

11 REPLIES 11

Siddhesh Gawade
Mega Sage
Mega Sage

Hello Rakesh,

 

You can use below syntax to set cc in email notification.

 

 email.addAddress("cc","email_address_of_the_user","Name_of_ther_user");

Kindly mark my answer as Correct and helpful based on the Impact.

Regards,

Siddhesh

Hi @Siddhesh Gawade 

Thanks for the reply. But this email address field value entered by itil user so he may enter manually into that field or he may select users watchlist field or he may enter email address and select few users in users list type field also. 

We need all the selected users in "cc".

You've split the list collector field into an array, which holds either sys_ids of existing users or the email address of those entered manually. So you have no access to the name if the user, but you can try this:

 

var getListusers = current.u_users.split(","); 
for(i=0; i<getListusers .length; i++) {
  email.addAddress("cc", getListusers[i]);
}

 

I actually don't know if it works without the display name, but I don't see a reason why it wouldn't. let me know.

Hi @Laszlo Balla ,

Thanks for the reply. Tried your suggested code but not working and even no logs are coming.

var getListusers = current.u_users.split(",");
for(i=0; i<getListusers .length; i++) {
gs.log("spl-->"+getListusers[i]);
email.addAddress("cc", getListusers[i]);
}