logo

NJP

Add an inbound email to a new record

Import · Jun 25, 2019 · article

When creating new records from an inbound email action, it can be handy to attach the original email to the record.

//This script will take contents from an inbound email and create an attachment on the created record from the inbound email action.   
var emailAsAttachment = new global.emailAsAttachmentUtil();
emailAsAttachment.create(email, current);

This Script Includes will add the inbound email as an attachment on the new

var emailAsAttachmentUtil = Class.create();
emailAsAttachmentUtil.prototype = {
    initialize: function() {
        this.newLineChar = "\n";   
        this.contentType = "text/html";
    },

    create: function(emailRec, currentRec){

        var emailDetails = "To: "+emailRec.to+ "\n";
        if(!gs.nil(emailRec.cc)){emailData.push("Cc: " + emailRec.cc);}
        emailDetails+= "Subject: " + emailRec.subject + "\n";
        emailDetails+= "From:"+ emailRec.origemail + "\n";
        emailDetails+= "X-Unsent: 1" + "\n";
        emailDetails+= "Content-Type: text/html" + "\n\n";
        emailDetails+= emailRec.body_html;

        var sysAttachment = new GlideSysAttachment();
        sysAttachment.write(currentRec, emailRec.subject+'.eml', "text/html", emailDetails);
    },

    type: 'emailAsAttachmentUtil'
};
View original source

https://www.servicenow.com/community/now-platform-articles/add-an-inbound-email-to-a-new-record/ta-p/2310948