The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to copy attachment from sys_email to case record

mania
Tera Contributor

Hi,

 

I am trying the copy attachment from sys_email to case record so i havee used below code but not getting mapped.

Can anyone please let us know where i missed?

(function executeRule(current, previous /*null when async*/) {

    // Add your code here
    var inc = new GlideRecord("sys_email");
    if (inc.get(current.table_sys_id)) {
        var grTask = new GlideRecord("sn_customerservice_case");
        if (grTask.get(inc.instance)) {
            copyAttachmentToTask(grTask.sys_id);
        }
    }


    function copyAttachmentToTask(conceptSysId) {
        // Get record from sys_attachment table
        var sourceAttachment = new GlideRecord('sys_attachment');
        sourceAttachment.addQuery("sys_id", current.sys_id);
        sourceAttachment.query();
        sourceAttachment.next();

        // Get field values from retrieved sys_attachment record
        var fileName = sourceAttachment.getValue('file_name');
        var contentType = sourceAttachment.getValue('content_type');
        var sourceAttachmentSysId = sourceAttachment.getValue('sys_id');

        // Attach sys_attachment record content stream to test_table record
        var gsa = new GlideSysAttachment();
        gsa.writeContentStream(
            grTask,
            fileName,
            contentType,
            gsa.getContentStream(sourceAttachmentSysId));
    }

})(current, previous);

Thanks!

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

@mania 

Is the email coming of type Reply and it has attachment and you want to add that file to target record?

If yes then do this in inbound action of type Reply

GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'sn_customerservice_case', current.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

@Ankur Bawiskar 

Thanks for the responding!

Even i used same line of code in forward inbound action also not getting mapped.

Thanks!

@mania 

it should work fine, did you add logs and see?

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

@Ankur Bawiskar 

Thanks you!

Grate, It worked.

Thanks!