Script to add cc'd users to watch list

mkader
Kilo Guru

Hello,

I need to create an inbound action that auto populates the watch list with  cc'd user(s) for RITMs. How can I do this? If a cc'd user does not exist in the system, I do not want to create a new user

Thanks!

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

Within this inbound action, in the script section, you can retrieve the "copied" users (aka CC'd) which will give you a comma separated string of their email address. From there, you can query your sys_user table to find a match based off of this and if found, add them to your watch list.

Something like:

var copied = email.copied;
var watchList = current.watch_list;
var findMem = new GlideRecord('sys_user');
findMem.addQuery('email', 'IN', copied);
findMem.addQuery('sys_id', 'NOT IN', watchList);
findMem.query();
while (findMem.next()) {
current.watch_list += "," + findMem.sys_id;
}
current.update();

I just tested this myself and it does work. So as long as your inbound action has found the correct record to associate the email to, this will work just fine.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

23 REPLIES 23

Damian Martinez
Mega Sage

Hello Everyone, I'm using the code suggested above.

var copied = email.copied;
var watchList = current.watch_list;
var findMem = new GlideRecord('sys_user');
findMem.addQuery('email', 'IN', copied);
findMem.addQuery('sys_id', 'NOT IN', watchList);
findMem.query();
while (findMem.next()) {
current.watch_list += "," + findMem.sys_id;
}
current.update();

However when I open an incident from email it creates two incidents:

find_real_file.png

the latest with no short description is the one that has the watcher list user, the one with short description does not have watch list user.

Do you know why this happens?

By the way I'm also trying to make this work in my company dev instance against the case table using the same code but does not work, what is the IN, NOT IN in the code, is this related to incident table?

Thanks.

 

Hi,

I'd recommend creating your own question so that the conversation can be useful/helpful to your unique scenario instead of in another post with a correct answer already marked from about a year ago where many users won't check to see what's going on. Your issue isn't due to script posted, but would be because of something else.

Feel free to tag me there using @Allen Andreas and I can assist.

Please mark reply as Helpful, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

emorganisc
Tera Contributor

Reviving an old thread, but the code posted by the others did not have the full functionality needed for me. Here is my solution, tested and fully functional. I posted this code inside the OOB "Update Case via reply" Inbound Action:

 

if (email.copied) {
	try {
		//Variables to instantiate any of the arrays needed for value checking.
		var ccUsers = email.copied;
		var sysUserIds = [];
		var sysUserEmails = [];
		var externalEmails = [];
		var ccEmails = ccUsers.split(',');

		//Query any system contacts, if they exist, from the CCed emails, then iterate over each one in the CC to check.
		var sysUserQuery = new GlideRecord('sys_user');  	
		for (var i = 0; i < ccEmails.length; i++) {
			var email = ccEmails[i].trim();
			if (email) {
				sysUserQuery.addQuery('email', email);
			}
		}
		sysUserQuery.query();
		//Iterates over each result of the query and adds each the sys_id and their associated email to arrays for
		// adding to watchlist and checking for its existence in the final array if they are system contacts.
		while (sysUserQuery.next()) {
			sysUserIds.push(sysUserQuery.getValue('sys_id'));
			sysUserEmails.push(sysUserQuery.getValue('email'));
		}
		//Takes current watch list and then checks to see if any system contacts are any of the external emails, 
		// if not, then pushes only non system emails to the external email array.
		var existingWatchList = current.watch_list.toString().split(',');
		for (var j = 0; j < ccEmails.length; j++) {
			email = ccEmails[j].trim();
			if (email && sysUserEmails.indexOf(email) === -1 && existingWatchList.indexOf(email) === -1) {
				externalEmails.push(email);
			}
		}
		//Combine system and non system emails into one array and 
		// removes any duplicates that could have been on existing watch list.
		var combinedList = existingWatchList.concat(sysUserIds, externalEmails);
		var arrayUtil = new global.ArrayUtil();
		var uniqueList = arrayUtil.unique(combinedList);
		//Joins the array into a string of commas separated values for putting into the watchlist.
		current.watch_list = uniqueList.join(',');
		current.update();
	} catch (ex) {
		gs.error("An error occurred: " + ex);
	}
}

 

Thank You for this.

I tested and can confirm that this works for me. I have been looking all over for this. How do exclude an email in the CC for example:

someone might email Jon Doe and have the helpdesk email in the CC.
When I test this on my end. the helpdesk email appears in the Watchlist and we do not want this.