Inbound Actions - Getting the email sysid

jwawa1
Kilo Contributor

We occasionally have users Reply to catalog request notifications, which we currently have no process in place to handle. We've considered creating a new Inbound Action to update the activity log of the request, but since our ITIL users don't work from catalog requests (they work from catalog tasks), the replies would go unnoticed. Instead, we'd like to fire an email off to notify our service desk manager when one of these unprocessed emails is received by service-now. In that notification, we'd like to include as much information from the original email as possible, as well as the number for the related record that was replied to.

The main problem I can't figure out is how to get the sysid of the email record being processed in the inbound email action, so we can pass that to an event and send a notification. I'm open to other ideas on how to address this scenario (note, it's not only on sc_requests though. Same scenario could happen if a user replies to a change request notification, or a survey, or a closed incident, etc). But the question I'm hoping to get some insight into first is "How can you get the sysid of the current email, when inside an Inbound Action?". It doesn't appear to be as simple as "email.sys_id".

8 REPLIES 8

jwawa1
Kilo Contributor

Here's what I ended up doing for this, if anyone is interested...

I created a new Inbound Action to handle Replies on the sc_request table with a script similar to the following:



if (current.getTableName() == "sc_request") {
recSysID = current.sys_id;
recips = 'joe.user@company.com';

if (email.uid != undefined){
//find match for an email with a matching UID
var em = new GlideRecord('sys_email');
em.query('uid', email.uid);
em.orderByDesc('sys_created_on');
em.query();
if(em.next()) {
gs.eventQueue("unprocessed.inbound.email",em,recSysID,recips);
}
}
}


I then created a new Notification for this Event similar to the following:
Subject: "Unprocessed email received in Service-Now"
Table: sys_email
User field: event.parm2
Omit Watermark: Checked/Selected/Enabled
Message:


An unprocessed email has been received in Service-Now and needs attention.

<mail_script>
var recSysID = current.instance;
var rec = new GlideRecord(current.target_table);
rec.query("sys_id", recSysID);
rec.query();
if(rec.next()) {
var destinationUrl = gs.getProperty("glide.servlet.uri") + '/nav_to.do?uri=' + current.target_table + '.do?';
destinationUrl += 'sys_id=' + event.parm1;
}
if (rec.number.isNil() == false){
var urlText = '(' + rec.number + ')';
}
else {
urlText = '(Link)';
}
template.print('The received email may be related to the following ' + rec.getClassDisplayValue() + ' ' + '<a href=' + destinationUrl + '>' + urlText + '</a>.');
</mail_script>

<u><strong>Details from email</u></strong>:
<u>Received:</u><BR/>
${sys_created_on}
<u>From:</u><BR/>
${user}
<u>Subject:</u><BR/>
${subject}
<u>Body:</u><BR/>
<mail_script>
template.print(current.body);
</mail_script>


Nice thing about the notification and event is that they can be re-used for unprocessed inbound messages for other scenarios as well (e.g. when a user replies to an incident that has already been closed, when a user replies to a Survey request, etc.). For those scenarios, you just need to create an Inbound Action for each, but the inbound action code, the event & the notification can pretty much all be reused.


danzak
ServiceNow Employee
ServiceNow Employee

To get the sys_id of the email in an inbound email action script, you can try using:


  • sys_email.sys_id

younes_sebti
Kilo Sage

Hello,



I was actually working on it today.



Each email has an unique identifier called the message-id.


This information is stored on the sys_email table.



Now the work is to get it while the email is processed.


You can use the email object. With this object you can either extract the subject, the body or the headers. The headers contains the message_id



Part 1 : Extract the message id from the header (I'm sure you can improve the script)


  var header = email.headers;


// text to find on the header


  var tag = 'Message-ID:';


  var message_id_start = header.indexOf(tag);


  var message_id_sub = header.substring(message_id_start);


  var message_id_stop = message_id_sub.indexOf('\n');


  var message_id="";


  if (message_id_start!=-1 && message_id_stop!=-1){


// extract the message id


  message_id=message_id_sub.substring(tag.length,message_id_stop-1);



Part 2 query the email table


var incoming_email = new GlideRecord('sys_email');


  incoming_email.addQuery('message_id',message_id);


  incoming_email.orderByDesc('sys_created_on');


  incoming_email.query();


  if (incoming_email.next()){


  /// do want you want here


  }


  }



Hope this can help you or anybody else


Thank you Younes! younes.sebti


I needed help to parse information from email.headers and I was able to use your script to accomplish my goal!   Thank you so much for posting this!