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

Erica2
Tera Contributor

Hello,

I successfully retrieved the email response with the attached file, which has been linked to the existing SCTASK record. However, the attachment is in the ".eml" format, and I was unable to obtain the original email in the ".msg" format.  Any help would be appreciated.

 

Erica2_0-1735266828401.png

 

 

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

Good morning @Ankur Bawiskar ,

Please see my response below. Thank you so much for your help!

Omkar Mone
Mega Sage

Hello Erica,

 

You can try making a change in the line with attachment.copy. Instead of using email.sys_id, replace it with sys_email.sys_id and check if it works. According to the documentation, attachments should ideally be copied to the target records. For reference, see ServiceNow Inbound Email Actions Documentation.

attachment.copy('sys_email', sys_email.sys_id, 'sc_task', taskGR.sys_id);