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

@mania 

Glad to know.

Please mark my below response as correct and close the thread.

AnkurBawiskar_0-1753758832029.png

 

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

@Ankur Bawiskar 

But i have forward inbound action that will work when we forward with any existing Case number it will map the body to worknotes else if not matching the case number it will create new record and it will map the body to worknotes so here i want to create a another function for copy attachments from sys_email to case ticket.

If i used this below line of code. It is getting duplications:

GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'sn_customerservice_case', current.sys_id);

For ex: If i add "A" image from email it should get to case form CS0001 and again if i add the same "A" image from email with the same record CS0001 it should not update.

Can you please help on this.

Thanks!

@mania 

then check if the file is already there then don't copy

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

@mania 

Hope you are doing good.

Did my reply answer your question?

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

Rafael Batistot
Kilo Patron

Hi @mania 

 

I could see some issues in your code 

 

  1. newGlideRecord is not valid – it should be new GlideRecord(...)
  2. Function declaration functioncopyAttachmentToTask is missing a space
  3. You’re calling grTask inside the function copyAttachmentToTask, but grTask is not available in its scope
  4. You’re using current.sys_id in sys_attachment.query() – this may refer to the sys_id of sys_email, not the attachment
  5. Your get(inc.instance) assumes instance refers to the case ID – that depends on your email configuration and may not be correct


May you try this one 

 

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

    // Get the email record
    var email = new GlideRecord("sys_email");
    if (!email.get(current.sys_id)) {
        return;
    }

    // Get the target Case record (assuming 'instance' field contains reference to sn_customerservice_case)
    var caseGr = new GlideRecord("sn_customerservice_case");
    if (!caseGr.get(email.instance)) {
        return;
    }

    // Copy all attachments from sys_email to the case record
    var attachmentGR = new GlideRecord('sys_attachment');
    attachmentGR.addQuery('table_sys_id', email.sys_id);
    attachmentGR.addQuery('table_name', 'sys_email');
    attachmentGR.query();

    while (attachmentGR.next()) {
        var gsa = new GlideSysAttachment();
        var contentStream = gsa.getContentStream(attachmentGR.sys_id);
        gsa.writeContentStream(
            caseGr,  // Destination record
            attachmentGR.file_name,
            attachmentGR.content_type,
            contentStream
        );
    }

})(current, previous);

 

  •  This script runs from a Business Rule or Email Inbound Action on the sys_email table.
  • The instance field of the sys_email record contains the sys_id of the target sn_customerservice_case record.
  • You want to copy all attachments from the email to the case.