Attachment on worknotes

swapnil15
Tera Contributor

Hi All,

I have a record on the sys_email table whenever an email is sent from the tool to the servicenow. These records have the body of mail in HTML format. When I right-click on it and click preview mail, then it shows me the whole proper body of the mail. I want to attach this mail body whole in the Work notes of the incident. Basically, I have written an inbound email action that creates an incident as soon as the XML format mail is received in the sys_email table. On the same new incident, the mail (which was seen when clicked on the preview email) should be attached to the incident work note.

Can anyone help me with this?

1 ACCEPTED SOLUTION

Hi @swapnil15 

Let's have a look into the post below.

Save Email Text as Attachment

 

make sure to replace the function createAttachment as following Scott20's comment.

 

function createAttachment(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("Headers: " + emailRec.headers + "Content-Type: multipart/mixed");
    emailData.push("");
    emailData.push("<html>");
    emailData.push("<body>");
    emailData.push(emailRec.body_html);
    emailData.push("</body>");
    emailData.push("</html>");

    // 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);
}

 

 

 

Cheers,

Tai Vu

View solution in original post

5 REPLIES 5

Sneh
Tera Contributor

I have the same requirement but we need data in html format in pdf.

 

I tried below code but it didn't work..

var EnrichmentDataAsAttachmentUtil = Class.create();
EnrichmentDataAsAttachmentUtil.prototype = {
    initialize: function() {
        this.newLineChar = "\r\n";  // Microsoft Windows expects \r and \n for return and new line
        //this.contentType = "text/plain";
        this.contentType = "text/html";
    },
   
    createAttachment: function (currentRec,data) {
        var fileName ='UserEnrichmentDataReport.pdf';
       
        // Setup array to push email values into.  Add additional as needed/
        var strData= [];
        strData.push("To: " + 'HI');
        strData.push("Subject: " + 'TEST');

        strData.push("<html>");
    strData.push("<body>");
    strData.push(data);
   
    strData.push("</body>");
    strData.push("</html>");
       
       
     
        var strngData= strData.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, strngData);
    //  sysAttachment.write(currentRec, fileName, this.contentType, data);
    },

    type: 'EnrichmentDataAsAttachmentUtil'
};
 
 
 
 
/// value of data which i'm passing in script include is :
 var data ='<div class="content">' +
          '<p>This Change has gone thru CAB review and has been Approved.</p>' +
          '<br />' +
          '<p>Once the Change has been implemented; please Update this Change Request with the following:</p>' +
          '<ol><li>Result of the Change - Successful or Not Successful</li><li>Description of work that was performed</li><li>Include any additional details</li><li>Optional for ExCAB - Description of the work that was performed</li></ol><br />' +
      '<p>The Implementor of the Change can either: </p>' +
      '<ol><li>Update the Change Request as a Work Note after implementation</li> <br><li>Join the next scheduled CAB meeting after implementation to discuss the outcome of the Change and update the Change Request as a Work Note during the CAB meeting</li></ol>' +
      '<br><p>Close Complete the Change</p>' +
          '</div>';
 
I'm getting below error:
 
Sneh_0-1702303282032.png

 

 
Regards,
Sneh