Copy unique attachments from case record to incident record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 02:55 AM - edited 11-22-2024 02:56 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 04:27 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 06:03 AM
Hey @Shantharao,
Every attachment has its Hash value which is the same for the duplicate attachments, but a unique Sys ID. The process follows:
- Copy all the files to the target record.
- 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