The CreatorCon Call for Content is officially open! Get started here.

Need help with Inbound email action script failed to attach file to the target record.

Erica2
Tera Contributor

Hello,

 

A user replies to an existing sctask email notification with an attachment. The inbound email is added email body text to the correct record, but the attachments are not getting attach. Below is the script I'm currently using.  Could someone please provide suggestion on how to get it to works.  Thank you

 

Erica2_0-1735245126666.png

 

// Extract the SCTASK number from the email subject 
var taskNumber = email.subject.match(/SCTASK\d+/);

if (taskNumber) {
    // Query the Task table for the matching SCTASK record 
    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('number', taskNumber[0]);
    taskGR.query();

    if (taskGR.next()) {
        // Add email body to the Work Notes or Comments 
      //  taskGR.comments = email.body_text;
		taskGR.work_notes = email.body_text;

        // Attach files from the email to the SCTASK 
        var attachment = new GlideSysAttachment();
        attachment.copy('sys_email', email.sys_id, 'sc_task', taskGR.sys_id);

        // Update the SCTASK record 
        taskGR.update();
    }
}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Erica2 

are you saying user had attached some file such as word, pdf etc and it's not getting attached?

your code should attach it to target record

update as this, attachment copy should be after the update without which you won't get sysId of record

 

// Extract the SCTASK number from the email subject 
var taskNumber = email.subject.match(/SCTASK\d+/);

if (taskNumber) {
    // Query the Task table for the matching SCTASK record 
    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('number', taskNumber[0]);
    taskGR.query();

    if (taskGR.next()) {
        // Add email body to the Work Notes or Comments 
      //  taskGR.comments = email.body_text;
		taskGR.work_notes = email.body_text;

        // Attach files from the email to the SCTASK 
        var attachment = new GlideSysAttachment();
        // Update the SCTASK record 
        taskGR.update();
        attachment.copy('sys_email', sys_email.sys_id, 'sc_task', taskGR.sys_id);
    }
}

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Good morning @Omkar Mone ,

 

Thank you for your assistance. I was able to retrieve the email response along with the attached file, and it has been linked to the existing SCTASK record correctly. 

 

However, the issue I'm encountering is that when I save the Outlook email and attach it, the file extension changes from ".msg" to ".eml," as shown in the images below. Is there a way to prevent this from happening?.  It is working fine with other file format such as word, pdf etc..., but not .msg

 

For example: I saved outlook emal file as .msg

Erica2_0-1735317011427.png

 

It shows in ServiceNow as message.eml.  Even the file name got changed.

Erica2_1-1735317113672.png

 

 

 

@Erica2 

This typically happens because ServiceNow's email processing system is designed to handle .eml files more effectively.

within ServiceNow it's handled with extension .eml

.msg extension is only for microsoft outlook

We cannot do much with this.

I hope I have provided a thorough answer to your question. I'm confident that with your developer skills, you can take it further from here.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader