Inbound action to populate watcher list from email CC?

Damian Martinez
Mega Sage

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:

find_real_file.png

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!

14 REPLIES 14

Hi Kieran, thanks for providing that code but it did not work for some reason.

Regards

You've indicated on a few replies its not working, what debugging have you done. The above is code I use in a production environment so is tested and functional

Hello Kieran,
I have been working as  servicenow administrator for a bit over a year with no developer skills, I'm not debugging code, I created a new inbound email action and trying different code examples from different community posts.

I tried your code above against incident table in my free personal developer instance.

is there a way to debug this in free personal developer instance?

are you using your code in inbound email action?

Thanks

Damian Martinez
Mega Sage

Hello @Allen Andreas  ,I got this code from:

https://community.servicenow.com/community?id=community_question&sys_id=9d818c771b1314546531ea89bd4bcb4d&anchor=answer_d94c2d841b4e8110be4955fa234bcb1d

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 @Damian Martinez 

Thanks for tagging me.

So, you have a few things going on here:

  1. You have this inbound action on the incident table (check your settings -- we can't tell because there's no screenshot)? But you wanted it on the case table? So you'd need to make that switch yourself in the inbound action settings for this.
  2. You most likely don't have the "stop processing" checkbox checks for this inbound action, thus other inbound actions in a higher order number above this one, will run (which means the "catch-all" create incident inbound action is still processing, which explains why there's a second incident)
  3. The "IN" and "NOT IN" in the code is related to the sys_user table. So it's taking the email field from sys_user and looking at it using the CC's emails from your inbound email and finding user matches. It's also then looking at the watch list for the current record that was identified in the inbound action to ignore people who are already on the watch list (so duplicates aren't added).

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


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