- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
As you may know, ServiceNow has the ability to accept an inbound email and perform some action. Inbound email actions allow you to configure what happens when the instance receives an email. Any attachments on the email will automatically get attached to the record created by the email. If the created record is of type task, the activity stream will also contain a link to the contents of the inbound email as well:
While the activity stream is useful, it can grow quite long and the records don't stay around forever. There have been a few posts on this community asking for the ability to create an attachment with the contents of the email and attach it to the record. This solution by @warrem inspired me to create this blog post outlining steps to solve this use case since some users have posted followup questions.
ServiceNow includes an attachment API that allows you to create an attachment from any data. For example, this API is leveraged by the workflow editor's Attachment Note activity which allows you to create a task attachment from a string of text. This solution will leverage this API and can be called by adding a few lines of code to your existing inbound email actions.
Steps:
- Navigate to System Definition \ Script Includes and click New.
- Set the following values:
-
- Name: emailAsAttachmentUtil
- Accessible from: All application Scopes = this will allow it to be called by all applications
- Active: checked
- Description: You may want to set the description to something like the following to document what this script includes does and how to call it
This utility script will take contents from an inbound email and create an attachment on the created record from the inbound email action. To utilize this script, add the following lines at the end of the inbound email action script:
var emailAsAttachment = new global.emailAsAttachmentUtil();
emailAsAttachment.createAttachment(email, current);
-
- Script:
var emailAsAttachmentUtil = Class.create();
emailAsAttachmentUtil.prototype = {
initialize: function() {
this.newLineChar = "\r\n"; // Microsoft Windows expects \r and \n for return and new line
this.contentType = "text/plain";
},
createAttachment: function (emailRec, currentRec) {
var fileName = emailRec.subject + '.eml';
// Setup array to push email values into. Add additional as needed/
var emailData = [];
emailData.push("To: " + emailRec.to);
emailData.push("Subject: " + emailRec.subject);
emailData.push("From: " + emailRec.origemail);
emailData.push(emailRec.body_text);
// Convert emailData to a string separated by new line character.
var emailString = emailData.join(this.newLineChar);
// Create attachment with email string and attach it to the record creatd by the email.
var sysAttachment = new GlideSysAttachment();
sysAttachment.write(currentRec, fileName, this.contentType, emailString);
},
type: 'emailAsAttachmentUtil'
};
-
- Click Submit
Your script include should look similar to the following:
- Navigate to System Policy \ Email \ Inbound Actions and open the one that you want to capture the contents of the email as an attachment.
- Go to the Actions Tab and scroll to the bottom of the script and paste in the following:
var emailAttachment = new global.emailAsAttachmentUtil();
emailAttachment.createAttachment(email, current);
- Click Update
Now any email that is processed by this inbound email action will log the contents as an attachment:
Things to be aware of:
- You can add this functionality to any inbound action
- The Email's To, Subject, From, and Body are captured in the attachment.
-
- If there are other fields you wish to capture feel free to edit lines 13-16 of the emailAsAttachmentUtil Script Include
- Line 4 of the emailAsAttachmentUtil Script Include sets the new line character of the text file. Windows requires \r (carriage return) and \n (new line) to show correctly in Notepad as an example. Feel free to edit for your environment.
- 12,308 Views
- « Previous
-
- 1
- 2
- 3
- Next »
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.