Inbound email action - Incident not created for emails that DO NOT have Signature with images

Chris Dea1
Tera Contributor

Hi all,

I am working on a customization of the out of box "Create Incident" Inbound Action; to allow customers to create incidents via email.  I have the requirement to get assignment_group and assigned_to as parameters and then automatically assign the new incident depending on the parameters on the email received by the instance.  Also the Inbound Action must create incidents whether it is a HTML email with signature images and file attachments, or a plaintext email (with no attachments).

For example, if email has assignment_group:WebDevelopers and assign_to:Jane Doe, then when the email is received an incident will be created and be assigned to WebDevelopers group and assigned to ITIL user named Jane Doe.  It should create incident no matter if it's HTML (with signature and attachment), or plaintext email.  Both types would have assignment_group:WebDevelopers and assign_to:Jane Doe parameters though....

I made some customized script and used if statement logic to check email content type and process the parameters, and initially it seems to work.  BOTH HTML emails with signature/images and file attachments worked (created incident and assigned properly), and also plaintext emails also worked (created and assigned properly) incident.

Now...I have some bizarre issue where if the email does NOT have email signature with images, then no incident is created!   Does anyone have an idea what is going on?  Why would there only be an issue for some emails (HTML emails with no signature/images?

Initially, I thought that maybe the issue is related to content type changing when the email does not have signature with images, but it's "multipart/mixed" when it does not create incident.  And it's also "multipart/mixed" for when emails that have email signature/images where it successfully create incidents.  ....

Here's the script I am using on Inbound Action for Create Incident (see requirements above):

 

//  Note: current.opened_by is already set to the first UserID that matches the From: email address

current.caller_id = gs.getUserID();

current.short_description = email.subject;


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


//var groupName = email.body.assignment_group;

if (email.content_type.includes("multipart")) {
    if (email.body.assignment_group != "") {
        var groupName = email.body.assignment_group;
        var gr = new GlideRecord("sys_user_group");
        gr.addQuery('name', groupName);
        gr.query();
        if (gr.next()) {
            var groupSysID = gr.sys_id;
            gs.addInfoMessage("found group" + gr.sys_id);
            current.assignment_group = groupSysID;
        }
    }


    if (email.body.assign_to != "") {
        var agentName = email.body.assign_to;
        var userGr = new GlideRecord("sys_user");
        userGr.addQuery('name', agentName);
        userGr.query();
        if (userGr.next()) {
            var userSysID = userGr.sys_id;
            gs.addInfoMessage("found user" + userGr.sys_id);
            current.assigned_to = userSysID;
        }
    }

    current.comments = "received from: " + email.origemail + "\n\n" + email.body_text + "\n\n====================\n\n" + "groupSysID: " + groupSysID;

} else if (email.content_type.includes("text")) {
    var blah = "something";
    if (email.body.assignment_group != undefined) {
        var groupNametext = email.body.assignment_group;
        var groupGr = new GlideRecord("sys_user_group");
        groupGr.addQuery('name', groupNametext);
        groupGr.query();
        if (groupGr.next()) {
            var groupSysIDtext = groupGr.sys_id;
            gs.addInfoMessage("found group" + groupGr.sys_id);
            current.assignment_group = groupSysIDtext;
        }
    }


    if (email.body.assign_to != undefined) {
        var agentNametext = email.body.assign_to;
        var uGr = new GlideRecord("sys_user");
        uGr.addQuery('name', agentNametext);
        uGr.query();
        if (uGr.next()) {
            var userSysIDtext = uGr.sys_id;
            gs.addInfoMessage("found user" + uGr.sys_id);
            current.assigned_to = userSysIDtext;
        }
    }

    current.comments = "received from: " + email.origemail + "\n\n" + email.body_text + "\n\n====================\n\n" + "groupSysIDtext: " + groupSysIDtext;

} else {
    var blahblah = "something";
    if (email.body.assignment_group != undefined) {
        var grpName = email.body.assignment_group;
        var grpGr = new GlideRecord("sys_user_group");
        grpGr.addQuery('name', grpName);
        grpGr.query();
        if (grpGr.next()) {
            var grpSysID = grpGr.sys_id;
            gs.addInfoMessage("found group" + grpGr.sys_id);
            current.assignment_group = grpSysID;
        }
    }


    if (email.body.assign_to != undefined) {
        var repName = email.body.assign_to;
        var repGr = new GlideRecord("sys_user");
        repGr.addQuery('name', repName);
        repGr.query();
        if (repGr.next()) {
            var uSysID = repGr.sys_id;
            gs.addInfoMessage("found user" + repGr.sys_id);
            current.assigned_to = uSysID;
        }
    }

    current.comments = "received from: " + email.origemail + "\n\n" + email.body_text + "\n\n====================\n\n" + "grpSysID: " + grpSysID;

}

//if (email.body_html.assign != "")
//   current.assigned_to = email.body_html.assign;

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


//current.comments = "received from: " + email.origemail + "\n\n" + email.body_text + "\n\n====================\n\n" + "groupSysID: " + groupSysID + "groupSysIDtext: " + groupSysIDtext + "blah: " + blah;


current.description = email.body_text;


current.insert();

 

I am attaching screenshots of the email logs for example of when it works (with email signature) and when it does not work (no email signature).  Also providing the customized script (above) I am using on the Inbound Action.  Someone please save me from tearing my hair out :)!  Have someone seen an issue like this before with Incoming Actions?  What could be the source of the issue?  I am a newbie with scripting in ServiceNow so take it easy on me if I am doing something obviously wrong :).

Thanks,

Chris

P.S. - i forgot to mention that in the case where no incident was created (for email without signature) you will see on the screenshot (orange highlight) that it says an incident was created (INC0016312) but when I check open incidents, nothing is created.

 

4 REPLIES 4

Willem
Giga Sage
Giga Sage

Hi Chris,

Have you used log statements/debugging to find out what parts of the script are executed and which if/else it enters?

Have you checked the instance's system logs for any info or errors?

In addition, are you validating/searching the Incident as admin? Is security allowing you to see al types of incidents? If it is assigned to a different group, could it not be visible then?

 

Hi Willem, thanks for the reply. I inserted some “blah” variables in the script and printed to comments and it seems the correct if is executing for mixed when the script executes. I have been reviewing email and system logs and so far cannot see evidence of what is going on. And I have admin access and could not find the incident even after doing global search (the number is taken though); if I test with email WITH signature then the next INC number is used.

After reading more, do you think this could be related to execution order or the “stop processing” option? I was thinking of changing the problematic “Create Incident”Incoming Action to have the higher or highest execution order (low number) and select “stop processing” in case some other Inbound Action was stopping the record from getting inserted for some reason.

Thanks,

Chris

Hi Chris,

You can of course give that a try, however, I do not think that will help.

As your log shows the 'Create Incident - 2' is processed.

 

Try simplifying your script. Start with just creating the incident with no specific content and add fields/values from there. Logging al the steps/values along the way. This way you can determine at what line the script is failing.

If you use the default 'create incident' inbound email action, does that work for emails that do not have the signature image?

I found the issue.   Turns out it was a problem with my first if statement section handling the mixed content emails.    Instead of...

email.body.assignment_group != ""

I changed to...

email.body.assignment_group != undefined

I made similar change for the assign_to if statement for the mixed content section. 

I also had to correct some code for handling when no assign_to is provided as a parameter on the inbound email; to set it to our service desk group as a default.

 

Best,

Chris