Copy unique attachments from case record to incident record

Shantharao
Kilo Sage

 

 

Hi All,

When I use GlideSysAttachment.copy() option duplicate attachments are copying when I attached attachment twice on the case record
I want to cascade only unique attachments from case record to incident record

I have used below script but not working 

var recordId = current.table_sys_id;
    var tableName = current.table_name;

    var source = new GlideSysAttachment();
    var source_content = source.getContentStream(current.sys_id);


    var targetGlideRecord = new GlideRecord("incident");
    targetGlideRecord.addQuery("parent", recordId);
    targetGlideRecord.query();
    while (targetGlideRecord.next()) {

        var fileName = current.file_name;
        var contentType = current.content_type;
        var sourceAttachmentSysId = current.getValue("sys_id");
        var targetId = targetGlideRecord.sys_id.toString();
        var targetTbl = targetGlideRecord.getTableName();

        var attachment = new GlideSysAttachment();
        attachment.writeContentStream(targetGlideRecord, fileName, contentType, source_content)

    }
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Shantharao 

here is the solution for that

Select specific attachments to 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

Muhammad Hamza
Kilo Sage

Hey @Shantharao,

Every attachment has its Hash value which is the same for the duplicate attachments, but a unique Sys ID. The process follows:

  1. Copy all the files to the target record.
  2. Then delete the duplicate files from the target record.

You can try this script to find the duplicate files on a record and then delete the duplicate records.

// get all the attachments with the count of duplicates only
var attachment = new GlideSysAttachment();

var agr = attachment.getAttachments('incident', '57af7aec73d423002728660c4cf6a71c');

var dict = {};

while (agr.next()) {
    var count = 0; // counting only the duplicates
    var sysIDs = [];
    if (dict.hasOwnProperty(agr.getValue('hash'))) { // check if already exists in the object
        count = dict[agr.getValue('hash')][1] + 1; // increase the counter
        sysIDs = dict[agr.getValue('hash')][0]; // get the Sys IDs array


    } else {
        count = 0;
    }
    sysIDs.push(agr.getValue('sys_id'));

    dict[agr.getValue('hash')] = [sysIDs, count];
}

// delete the duplicate attachments

for (i in dict) {
    // we will only get the attachments where the count is greater than 0
    if (dict[i][1] > 0) {
        for (var j = 0; j < dict[i][1]; j++) {  // run only to the count
            var attachmentDelete = new GlideSysAttachment();
            var attachmentSysID = dict[i][0][j]; // getting the sys id of the duplicate attachment
            attachmentDelete.deleteAttachment(attachmentSysID);

        }
    }
}

This way you can get rid of the duplicate attachments.

 

Note: I tested in my PDI on Xanadu.

 

Kindly appreciate the efforts of community contributors by marking appropriate response as the correct solution and helpful, this may help other community members to follow the right solution in the future.

 

Thanks,

Hamza