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

Thank you!!



If you found my response helpful, please mark it as correct and close the thread to benefit others.
Regards,
Kaustubh

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