How to copy the attachments from inbound email to the request and its related records

Arundhati
Tera Contributor

Hello everyone,

I have a little bit complicated situation i am currently adding the inbound email as an worknote or comments to the Incident, Change, Request(RITM and SCTASK as well)

But while updating the request I am having a condition to update the related records as well so when the sctask is updated it should update RITM and request and when RITM is updated it should update Request and Sctask and so on  

Everything is good until the email comes in with attachments and inline images 

so when the email is received by RITM on the RITM everything is fine but for the SCTASK and Request if it already contains any images it copies every previous image and attachment together with image and attachments received from the new email how do i solve this issue below is my code to copy the attachment

 

_moveAttachments: function(emailSysId, tableName, recordSysId, requesterFlag, hasUOId) {

        var gsa = new GlideSysAttachment();

  
        var beforeIds = {};
        var before = new GlideRecord("sys_attachment");
        before.addQuery("table_name", tableName);
        before.addQuery("table_sys_id", recordSysId);
        before.query();
        while (before.next()) {
            beforeIds[before.sys_id + ""] = true;
        }
        gsa.copy("sys_email", emailSysId, tableName, recordSysId);

        
        var after = new GlideRecord("sys_attachment");
        after.addQuery("table_name", tableName);
        after.addQuery("table_sys_id", recordSysId);
        after.query();

        while (after.next()) {
            var attId = after.sys_id + "";

            // Skip attachments that already existed
            if (beforeIds[attId])
                continue;

            if (!after.isValidField("u_customer_visible"))
                continue;

          
            if (hasUOId) {
                after.u_customer_visible = false;
            } else {
                after.u_customer_visible = requesterFlag === true;
            }

            after.update();
        }
    },
 
3 REPLIES 3

Ankur Bawiskar
Tera Patron

@Arundhati 

copy method copies all files again and not specific one

use this link where I shared solution to copy specific file

Is there any way to copy single attachment from multiple attachments ?? 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hi Ankur,

My requirement is if RITM is updated with email then copy the data to SCtask as well not just single attachment but when RITM is updated its just once and SCTASk Copies all other and vice versa and unfortunately this does not solve my problem

 

@Arundhati 

please share complete business requirement with screenshots

what's happening currently?

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