Skip to content

Commit b013688

Browse files
authored
Merge pull request #1 from swatii23/swatii23-patch-1
Duplicate Attachment Finder
2 parents 65603ae + 6d8bb55 commit b013688

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Duplicate Attachment Finder
2+
3+
This script identifies duplicate attachments in a ServiceNow instance based on file name and size, and lists them in descending order of creation date.
4+
5+
## Features
6+
7+
- Finds attachments with the same **file name** and **size**.
8+
- Orders duplicates by **creation date (latest first)**.
9+
- Shows the **attachment Sys ID**, **parent table**, and **record**.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
(function() {
2+
var attachmentGr = new GlideRecord('sys_attachment');
3+
attachmentGr.orderByDesc('sys_created_on');
4+
attachmentGr.query();
5+
6+
var attachments = {};
7+
var duplicateList = [];
8+
9+
while(attachmentGr.next()) {
10+
var key = attachmentGr.file_name + '|' + attachmentGr.size_bytes;
11+
if(!attachments[key]) {
12+
attachments[key] = [];
13+
}
14+
attachmentGr[key].push({
15+
sys_id: attachmentGr.sys_id.toString(),
16+
table_name: attachmentGr.table_name.toString(),
17+
table_sys_id: attachmentGr.table_sys_id.toString,
18+
created_on: attachmentGr.sys_created_on.toString()
19+
})
20+
}
21+
22+
for (var i in attachments) {
23+
if(attachments[i].length > 1) {
24+
duplicateList.push({
25+
file_key: i,
26+
count: attachments[i].length,
27+
records: attachments[i]
28+
})
29+
}
30+
}
31+
32+
if (duplicateList.length === 0) {
33+
gs.info('No duplicate attachments found.');
34+
} else {
35+
gs.info('Found ' + duplicateList.length + ' duplicate attachments:');
36+
duplicateList.forEach(function(dup) {
37+
gs.info('File: ' + dup.file_key + ' | Count: ' + dup.count);
38+
dup.records.forEach(function(r) {
39+
gs.info(' Sys_ID: ' + r.sys_id + ' | Table: ' + r.table_name + ' | Record: ' + r.table_sys_id + ' | Created On: ' + r.created_on);
40+
});
41+
});
42+
}
43+
}());

0 commit comments

Comments
 (0)