Inbound action: Forward emails aren't updating

Aaron40
Kilo Guru

I have a very simple FORWARD inbound action. It's not updating the incident that it finds, in fact it creates a new incident 100% of the time. All my test prints show up in the logs, so the action is taking place and it's detecting the watermark MSG string.

Why is it creating a new record instead of updating? I don't have a single mention of an insert anywhere.

Type: Forward



gs.include('validators');
if (email.to.toLowerCase().indexOf('email@here.com') > -1)
{
             gs.log("TEST: caught script");
// First thing we'll do is check for a watermark
if (email.body_text.indexOf("Ref:MSG") >= 0)
{
             gs.log("TEST MSG FOUND");
current.comments = "FORWARD WITH WATERMARK: " + email.origemail + "\n\n" + email.body_text;
var incnum = current.update();
gs.log("TEST: updated incident " + incnum);
}
}
6 REPLIES 6

Same here... helped me as well.   Here is an updated link to the doc's site since the wiki is gone.



https://docs.servicenow.com/bundle/jakarta-servicenow-platform/page/administer/notification/concept/...


Dan Covic2
Tera Contributor

For me this script does the job:

 

gs.include('validators');

var watermarks = email.body_text.match(/Ref:MSG.*/gi);
var refMsgArray = (watermarks) ? watermarks[watermarks.length - 1].split(":")[1] : false;
if (refMsgArray) {
    var mark = new GlideRecord('sys_watermark'); // check for a watermark
    mark.addEncodedQuery('number=' + refMsgArray);
    mark.query();

    if (mark.next()) {
        var inc = new GlideRecord(mark.source_table); // get the incident record by the table name and sys_id
        inc.get(mark.source_id); // use dot-walking to get the incident number
        if (inc.isValidRecord()) { // checking if the record exists 
            inc.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;
            inc.update();
        }
    }
} else {
    current.caller_id = gs.getUserID();
    current.comments = "forwarded by: " + email.origemail + "\n\n" + email.body_text;
    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 (current.canCreate())
        current.insert();
}

The script prevents new tickets (duplicates) from being created when a user forwards an email from ServiceNow. So, the user feedback is going to be included in the existing ticket when a user forwards an email from ServiceNow. Otherwise (when the user forwards it without a watermark), the script creates a new INC