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

Hello @Allen Andreas ,
Thanks for your fast reply, I appreciate it, first I'm trying to make this work against incident table in my free personal developer instance.
Here are the screenshots about the inbound email action:

find_real_file.png

 

find_real_file.png

The code is this:

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

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();
    
})(current, event, email, logger, classifier);

 

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?

 

 

Hi,

Unfortunately, you sort of repeated most of what you've already said. I'm unsure if you read my reply or not.

However, with the screenshots, we can see that the stop processing checkbox for this inbound action is actually checked, but the order number is still 100, which is the same order number as the "catch-all" inbound action for the same table. You'd want to set this order number to 99 or lower.

Then run the test again.

The rest of your questions are already answered because you have 2 inbound actions doing 2 things. One, is just taking the CC's and adding them to the watch list, which is working...and the other is the out of box "catch-all" incident inbound action which does a few other things, but doesn't touch the CC to watchlist functionality that is set in this inbound action.

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


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

Hello @Allen Andreas,
thanks so much for your clarification above, so about inbound email actions, do they all run in order until one of them stops processing?

I lowered the number of the Add CC to Watchlist inbound email action and unchecked stop processing, still two incidents are created, if I checked stop processing, only one incident is created with no short description,  what I don't understand is why is this inbound email action creating an incident? The code is just asking to add a user to watch list, not  to create incident?

There is Create Incident inbound email action, shall I add the code of the my new inbound action to this one, so it creates the incident and add the watcher at the same time it creates one incident?

Also there is an Update Incident inbound email action, can I add the code here too? the reason why is because if I reply from email to a new comment maybe I want to add a new watcher.

Once I get all this working, I'll set this up in my company dev instance against case table.

Thanks.

Hello @Allen Andreas ,

I finally got this working, I updated the Create Incident and Update Incident inbound email actions and modify the code as follows:

Create Incident:

//    Note: current.opened_by is already set to the first UserID that matches the From: email address
var copied = email.copied;
var watchList = current.watch_list;
var findMem = new GlideRecord('sys_user');

current.caller_id = gs.getUserID();
current.comments = "received from: " + email.origemail + "\n\n [code]" + email.body_html + "[/code]";
current.short_description = email.subject;

current.category = "inquiry";
current.incident_state = IncidentState.NEW;
current.notify = 2;
current.contact_type = "email";

if (email.body.assign != undefined)
   current.assigned_to = email.body.assign;

if (email.importance != undefined) {
   if (email.importance.toLowerCase() == "high") {
        current.impact = 1;
        current.urgency = 1;
   }
}

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

current.insert();

Update Incident:

gs.include('validators');
var copied = email.copied;
var watchList = current.watch_list;
var findMem = new GlideRecord('sys_user');


if (current.getTableName() == "incident") {
    
    var gr = current;
    
    if (email.subject.toLowerCase().indexOf("please reopen") >= 0)
        gr = new Incident().reopen(gr, email) || gr;
    
    gr.comments = "reply from: " + email.origemail + "\n\n [code]" + email.body_html + "[/code]";
    
    if (gs.hasRole("itil")) {
        if (email.body.assign != undefined)
            gr.assigned_to = email.body.assign;
        
        if (email.body.priority != undefined && isNumeric(email.body.priority))
            gr.priority = email.body.priority;
    }
    
findMem.addQuery('email', 'IN', copied);
findMem.addQuery('sys_id', 'NOT IN', watchList);
findMem.query();
while (findMem.next()) {
current.watch_list += "," + findMem.sys_id;
}
    
    gr.update();
}

 

Next step is try this in my company dev instance against case table.

Hi,

Great!

You had numerous questions in your replies and I had been out of town and didn't get back until today, but they create incidents, because these inbound actions are on the incident table (that table was chosen in the settings).

If an inbound action is evaluated, the stop processing checkbox tells to the system to either stop....processing or don't (and after it's done with inbound action A -- keep seeing if other inbound actions also qualify to be ran for this same scenario).

So when you lowered the order number AND unchecked the box, yes, two incidents will result. When they're the same order number, both are ran, because both are at the same "level".

So to circle back, in addition to some code being changed, the main points were to adjust the order number, etc. which I hope I helped guide you Correctly.

If one of my replies guided you Correctly, please mark it as Correct.

Thanks and take care! 🙂


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