Inbound action to populate watcher list from email CC?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2022 06:44 AM
Hello everyone,
have somebody implemented en inbound email action that adds a CC user from email to the watch list field in case or incident form?
I created a new one but it is not working:
I added this code in the actions tab:
(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
var wList = current.watch_list;
var rarray = email.recipients_array;
var instanceEmail = gs.getProperty('glide.email.user');
for (var i = 0; i < rarray.length; i++) {
var recipient = rarray[i];
var gr = new GlideRecord('sys_user');
gr.addQuery('email', recipient);
gr.query();
if (gr.next()) {
// It's a user
if(wList != "") {
wList = (wList + "," + gr.sys_id);
} else {
wList = gr.sys_id;
}
} else {
//It's not a user either...so just add the address to the list...except instance email address
if (recipient != instanceEmail) {
if(wList != "") {
wList = (wList + "," + recipient);
} else {
wList = recipient;
}
}
}
}
//}
current.watch_list +=email.copied;
})(current, event, email, logger, classifier);
For some unknown reason it does not do anything
Thanks!
- Labels:
-
Agent Workspace

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2022 06:56 AM
Hi
Try this.
var rarray = email.recipients.split(",");
var instanceEmail = gs.getProperty('glide.email.user');
for (var i = rarray.length; i--;) {
if (rarray[i] === instanceEmail) {
rarray.splice(i, 1);
}
}
current.watch_list = rarray.toString();
Or
var arr = [];
var id = email.copied.split(',');
gs.log(' id is '+ id.length);
for(var i = 0 ; i < id.length ; i ++){
var gr = new GlideRecord('sys_user');
gr.addQuery('email','IN',id[i]);
gr.query();
while(gr.next()){
arr.push(gr.sys_id.toString());
}
}
current.watch_list = current.watch_list +','+arr.toString();
OR
current.watch_list +=email.copied;
or
current.watch_list = current.watch_list + ',' +email.copied;
Mark my answer as correct or hit like based on impact
Regards,
Musab

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2022 02:55 AM
Hello
none of the code above works, this is the only code that worke in my free personal development instance:
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:
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2022 03:04 AM
Hi,
var copied = email.copied; /// Not related to incident
var watchList = current.watch_list; /// Related to incident record.
Also add current.update() inside of while loop or first try with 'if' condition instead of 'while' condition
Regards,
Musab

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2022 03:24 AM
You can use Array.map to condense this code base into something more readable:
(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
logger.info("WatchList IA - email recipients: " + email.copied);
var copiedUsers = email.copied;
//if we're an empty value - no point going further
//If there isn't a watch_list on the target tbale - no point going further
if(gs.nil(email.copied) || !current.isValidField('watch_list'))
return;
var emailRecipientsRatified = copiedUsers.split(",").map(function(user){
var userGR = new GlideRecord('sys_user');
userGR.addQuery('email' , user);
userGR.setLimit(1);
userGR.query();
if(userGR.next()){
return userGR.getUniqueValue();
} else {
return user;
}
});
var existingWatchlist = curent.getValue('watch_list');
var unionedWatchlist = new ArrayUtil().union(emailRecipientsRatified , existingWatchlist.split(","));
current.setValue('watch_list' , unionWatchList.join(","));
})(current, event, email, logger, classifier);
This can also run in its own inbound action, so it applies to any record that comes in if you so wish.