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();
        }
    },
 
4 REPLIES 4

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

Hi ankur,

so my requirement is whenever system receives an email with an ticket number we need to update thoes email as an journal entry to the ticket so if its a custsomer email as an additional comment and if its an internal then as an worknote and generate an pdf for the email and attach it aswell to the ticket but for incident and change everything is okay.

But for Request we need to update the related records as well so if Request receives an email then i need to update RITM and SCTASK and if RITM receives the update SCTASK and request and same for SCTASK 

But here the issue is the email some times includes attachments, screenshot, inline image 

so when RITM recives an email RITM is updated normally only the new attachments are copied as an attachment but for SCTASK and Request all the attachments are getting copied that to multiple times

Below is the example where i sent an email to Request just an generated pdf

Arundhati_1-1778696055877.png

Arundhati_2-1778696131179.png

similarly at the same time multiple similar images got updated for RITM 

Arundhati_3-1778696188592.png

just with few updates you can see the count for ritm attachments and same happening with sctask

Hope its clear to you its a bit complex

regards,

Arundhati