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

Hii @Rakesh50 

For that you can store all the email addresses in one array and then you can use something like following to add all users in cc. If its user add get there email address by using glideRecord and store in array.

 

arr = ['example1@gmail.com', 'sexample2@gmail.com','example3@gmail.com'];

for(i=0; i<arr.length; i++){
    gs.info('in for loop')
var gr = new GlideRecord('sys_user');
gr.addQuery('email', arr[i]);
gr.query();
while(gr.next()){
email.addAddress("bcc", gr.email, gr.name);
}
}
 

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

Regards,

Siddhesh

@Siddhesh Gawade  Yes you are correct. I have tried same code which is similar to your code. But not works. Because users list type field returning sys_id. I have noticed that in logs. Tried below code

Rakesh50_0-1684512433012.png

if(!current.u_users.nil()){
var watcherIds = current.u_users.split(",");
gs.log("watcherIds-->"+watcherIds);
 var user = new GlideRecord("sys_user");
user.addQuery("sys_id", watcherIds);
user.addQuery("notification",2);
user.addQuery("email","!=","");
user.query();
while(user.next()){
email.addAddress("cc", user.email, user.getDisplayValue());}}

@Rakesh50 

If this the issue you can just separate the sys_ids from email addresses and using glideRecord you can get email addresses for those sys_ids as well. once you got all email addresses in one array you are good to go with the provided script.

 

 

@Siddhesh Gawade But i'm struck in separation. I want to know how to separate email id's from array and also i need these separated email address too for sending them in "CC" in notification.

Hello @Rakesh50 

You can for loop to check if the array idex contains some string which is similar in all emails

arr =[]//which contains all values including sys IDs and email addresses

Email=[];

SysID =[];

for (var i = 0; i < arr.length; i++) {

 

if(arr[i].contains("gmail.com"){//you can use any string value         

Email.push(arr.[i];

}

else{

SysID.push(arr[I];

}

}

After this you can separated sysids and email addresses. Then you can get emails for sysid array using GlideRecord and for loop. And then just join those two email address arrays. And then finally use script to set cc addresses.