inbound emails w/ attachments: attachment doesn't get added to created incident

jeff_furst
Kilo Contributor

I know that this is supposed to be OOB functionality. Inbound Email Actions have been stripped down to include just the following. I put the original Inbound email action back in and disabled this one, and the attachment was included in the incident ticket as it should.

Name: Automated Ticket Update 8: new INC
Business rule:
gs.include('validators');

automatedTicketUpdate();

Next, I went to business rules, inbound email functions. This contains code that is found in the original Inbound Email Action, plus a bunch of other "stuff" shown below. Can you tell me what part of this (if any) says to include the attachment with the newly created Incident or strip it?

-Thanks!

Script:
------------------------------------------------------------------------------------------------
function automatedTicketUpdate () {
// general pattern match
var pattern = /(INC|CHG|PRB|PRJ)([0-9]+)/;

var subject = email.subject;

var match = pattern.exec(subject);

if (match)
{
var ticketNum = match[0];

if(ticketNum.indexOf("INC")>-1)
{
var record = new GlideRecord('incident');
}

else if(ticketNum.indexOf("CHG")>-1)
{
record = new GlideRecord('change_request');
}

else if(ticketNum.indexOf("PRB")>-1)
{
record = new GlideRecord('problem');
}

else if(ticketNum.indexOf("PRJ")>-1)
{
record = new GlideRecord('pm_project');
}

record.addQuery('number',ticketNum);
record.query();

if(record.next())
{
gs.log("Inbound Email Action: Automated Ticket Update 8: ticketNumber query OK: " + record.number);

record.work_notes = "reply from: " + email.origemail + "\n\n" + "subject: " + email.subject + "\n\n" + email.body_text;
if (email.body.assign != undefined)
{ record.assigned_to = email.body.assign; }

if(email.body.assignment_group != undefined)
{ record.assignment_group = email.body.assignment_group; }

if(email.body.u_category != undefined)
{ record.u_category = email.body.u_category; }

if(email.body.u_type != undefined)
{ record.u_type = email.body.u_type; }

if(email.body.u_item != undefined)
{ record.u_item = email.body.u_item; }

if (email.body.priority != undefined && isNumeric(email.body.priority))
{ record.priority = email.body.priority; }

if (email.body.short_description != undefined)
{ record.short_description = email.body.short_description; }

if (email.body.u_outage != undefined)
{ record.u_outage = email.body.u_outage; }

if (email.body.company != undefined)
{
gs.log("Inbound Email Action: company values: " + email.body.company);
record.company.setDisplayValue(email.body.company);
}

record.update();
}
else // ticketNumber query returns null, reply to sender that ticket in subject does not exist
{
gs.log("Inbound Email Action: Automated Ticket Update: ticketNumber query NG");
gs.log("Inbound Email Action: Automated Ticket Update: sending email reply to inform user of error");

var rec = new GlideRecord('sys_email');

rec.initialize();
rec.mailbox = 'outbox';
rec.state = 'ready';
rec.recipients = email.origemail;
rec.subject = "Automated Ticket Update: Ticket " + ticketNum + " Not Found";
rec.body = "The ticket number " + ticketNum + " was not found in the database.\n\n" + "Please double check the ticket number you specify in the Subject refers to an existing ticket.\n\n" + "Only existing tickets can be updated using the automated ticket update email script.\n\n" + "Please report any errors by opening up a Problem ticket using the Service Now Web Portal.";
rec.insert();
}
}
else // if sender does not reference a ticket pattern in subject, it is an incident, create incident
{
var skipmsg = false;

var skip = /((address1|address2|address3)@(mpaddress.net|cisco.com))/i;
gs.log("regex = " + skip);
var matchSender = skip.exec(email.origemail);
gs.log("email.origemail = " + email.origemail + " email.subject = " + email.subject + " matchSender = " + matchSender );

if (matchSender)
{
gs.log("matchSender = true");
skipmsg = true; // Set skipmsg to true to skip the creation of a new incident
// Check if short description exists and updated in last hour, if so, append ticket
var insert_notes = "reply from: " + email.origemail + "\n\n" + "subject: " + email.subject + "\n\n" + email.body_text;
var threshold = 3600;
var gr = new GlideRecord('incident');

// strip out RE:'s from reply mails
email.subject = email.subject.replace(/re:? +|re:/gi,'');

gr.addQuery('short_description','=',email.subject);
gr.addQuery('incident_state','<','6');
gr.orderByDesc('sys_updated_on');
gr.query();

if(gr.next())
{
found = gr;
gs.log("Found ticket with matching subject: " + email.subject + " and number: " + found.number);

gs.log("dateDiff between " + gs.nowDateTime() + " and " + found.sys_updated_on.getDisplayValue());

var diff = gs.dateDiff(found.sys_updated_on.getDisplayValue(), gs.nowDateTime(), true);

gs.log("Diff: " + diff + ", threshold: " + threshold);

if (diff <= threshold)
{

gs.log("updating ticket: " + found.number);
gs.log("Comments: " + insert_notes);
found.work_notes = insert_notes;
found.update();

}
else
{
// outside of threshold but check if incident creation should be skipped due to good/restored
if ((subject.toLowerCase().indexOf("good") > -1 ) || (subject.toLowerCase().indexOf("restored") > -1 ))
{
skipmsg = true;
gs.log("Good/Restored found in message - ignoring this");
}
else
{
// outside of threshold and no good/restore
gs.log("Good/Restored not found in message - skipmsg = false");
skipmsg = false;
} // if
} // diff <= threshold

} // gr.next
// No existing incident found matching short description and updated in the last hour -- Create New Incident
else
{
gs.log("No existing inc found matching short_desc: " + email.subject + " updated in the last hour, creating new incident");
if (email.subject != undefined)
{
subject = email.subject;
if ((subject.toLowerCase().indexOf("good") > -1 ) || (subject.toLowerCase().indexOf("restored") > -1 ))
{
skipmsg = true;
gs.log("Good/Restored found in message - ignoring this");
}
else
{
skipmsg = false;
gs.log("Not a current incident - no append - no good - no restored - moving to SkipMsg Section");
}
}

}
} // matchSender if
else
{
if ((subject.toLowerCase().indexOf("good") > -1 ) || (subject.toLowerCase().indexOf("restored") > -1 ))
{
skipmsg = true;
gs.log("Good/Restored found in message - ignoring this");
}
} // matchSender else

if (!skipmsg)
{

record = new GlideRecord('incident');

record.comments = "reply from: " + email.origemail + "\n\n" + "subject: " + email.subject + "\n\n" + email.body_text;
record.short_description = email.subject;

var sid = gs.createUser(email.from);

record.caller_id = sid;
record.opened_by = sid;
record.category = "request";
record.incident_state = 1;
record.notify = 2;

if (email.body.assign != undefined)
{ record.assigned_to = email.body.assign; }

if(email.importance != undefined)
{ if(email.importance == "High")
{ record.priority = 1; } }

if (email.body.priority != undefined)
{ record.priority = email.body.priority; }

if (email.body.category != undefined)
{ record.category = email.body.category; }

if (email.body.u_outage != undefined)
{ record.u_outage = email.body.u_outage; }

if (email.body.company != undefined)
{ record.company.setDisplayValue(email.body.company); }

record.insert();

gs.log("creating new incident: " + record.number);

} // skipmsg
}
}
----------------------------------------------------------------------------------

3 REPLIES 3

jeff_furst
Kilo Contributor

OK, no replies. I still have not figured out what is stripping the attachment out of the incident that was created. Is there, perhaps, a tuturial somewhere that goes through in detail all of the email actions from within the prodcut? When rules does it follow and in what order?


Kostya
Tera Guru

hello, i had the same issue with the case numbers in the subjects. My solutions is very similar (Email interface quesiton#comment-17711), but i had no problems with attachments.

best regards
Konstantin


Hit the Thumb Icon and/or mark as Correct, if my answer was correct. So you help others to see correct responses and I get fame 🙂

Cheers,
Kostya

Nick65
Mega Expert

The code to copy attachments from emails to the record is not included/visible to us in the inbound action script. Also nothing you posted has any code that would copy the attachments.   Really old post I know, but here is a diagram to display the logic for email routing.


Inbound_email_action_type.png