|
1 | | -// Duplicate Record Finder |
2 | | -// Usage: Run in Scripts - Background or as a Fix Script |
3 | | -// Update the variables 'tableName' and 'fieldName' below before running |
4 | 1 |
|
5 | | -var tableName = 'incident'; // Set your target table here |
6 | | -var fieldName = 'short_description'; // Set the target field to check duplicates |
| 2 | +(function () { |
| 3 | + var tableName = 'incident'; // ✅ Set your target table |
| 4 | + var fieldName = 'short_description'; // ✅ Set your target field to check duplicates |
7 | 5 |
|
8 | | -findDuplicates(tableName, fieldName); |
| 6 | + findDuplicates(tableName, fieldName); |
9 | 7 |
|
10 | | -function findDuplicates(tableName, fieldName) { |
11 | | - // Validate that the table exists |
12 | | - if (!gs.tableExists(tableName)) { |
13 | | - gs.info('Table "' + tableName + '" does not exist.'); |
14 | | - return; |
15 | | - } |
| 8 | + function findDuplicates(tableName, fieldName) { |
| 9 | + // --- Validate Table --- |
| 10 | + if (!gs.tableExists(tableName)) { |
| 11 | + gs.error('❌ Table "' + tableName + '" does not exist.'); |
| 12 | + return; |
| 13 | + } |
16 | 14 |
|
17 | | - // Validate that the field exists on the table |
18 | | - var gr = new GlideRecord(tableName); |
19 | | - gr.initialize(); |
20 | | - if (!gr.isValidField(fieldName)) { |
21 | | - gs.info('Field "' + fieldName + '" does not exist on table "' + tableName + '".'); |
22 | | - return; |
23 | | - } |
| 15 | + // --- Validate Field --- |
| 16 | + var gr = new GlideRecord(tableName); |
| 17 | + gr.initialize(); |
| 18 | + if (!gr.isValidField(fieldName)) { |
| 19 | + gs.error('❌ Field "' + fieldName + '" does not exist on table "' + tableName + '".'); |
| 20 | + return; |
| 21 | + } |
24 | 22 |
|
25 | | - // Prepare GlideAggregate to find duplicates |
26 | | - var ga = new GlideAggregate(tableName); |
27 | | - ga.addAggregate('COUNT', fieldName); |
28 | | - ga.groupBy(fieldName); |
29 | | - ga.addHaving('COUNT', '>', 1); // More than 1 means duplicates exist |
30 | | - ga.addNotNullQuery(fieldName); // Ignore null or empty values |
31 | | - ga.query(); |
32 | | - |
33 | | - var duplicateCount = 0; |
34 | | - var duplicates = {}; |
35 | | - |
36 | | - while (ga.next()) { |
37 | | - var value = ga.getValue(fieldName); |
38 | | - var count = parseInt(ga.getAggregate('COUNT', fieldName), 10); |
39 | | - duplicates[value] = count; |
40 | | - duplicateCount++; |
41 | | - } |
| 23 | + // --- Use GlideAggregate for Efficient Counting --- |
| 24 | + var ga = new GlideAggregate(tableName); |
| 25 | + ga.addAggregate('COUNT', fieldName); |
| 26 | + ga.groupBy(fieldName); |
| 27 | + ga.addHaving('COUNT', '>', 1); // Find duplicate groups |
| 28 | + ga.addNotNullQuery(fieldName); // Ignore empty or null values |
| 29 | + ga.query(); |
42 | 30 |
|
43 | | - if (duplicateCount === 0) { |
44 | | - gs.info('No duplicates found for field "' + fieldName + '" on table "' + tableName + '".'); |
45 | | - return; |
46 | | - } |
| 31 | + var duplicateGroups = []; |
| 32 | + while (ga.next()) { |
| 33 | + var value = ga.getDisplayValue(fieldName); |
| 34 | + var count = parseInt(ga.getAggregate('COUNT', fieldName), 10); |
| 35 | + duplicateGroups.push({ |
| 36 | + value: value, |
| 37 | + count: count |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + // --- Logging Results --- |
| 42 | + if (duplicateGroups.length === 0) { |
| 43 | + gs.info('✅ No duplicates found for "' + fieldName + '" on "' + tableName + '".'); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + gs.info('⚠️ Found ' + duplicateGroups.length + ' duplicate groups for "' + fieldName + '" on "' + tableName + '".'); |
| 48 | + |
| 49 | + // --- Detailed Logging (optional for large datasets) --- |
| 50 | + duplicateGroups.forEach(function (item, index) { |
| 51 | + gs.info( |
| 52 | + (index + 1) + '. Value: "' + item.value + '" → Occurrences: ' + item.count |
| 53 | + ); |
| 54 | + }); |
47 | 55 |
|
48 | | - gs.info('Found ' + duplicateCount + ' groups of duplicates for field "' + fieldName + '" on table "' + tableName + '":'); |
49 | | - for (var val in duplicates) { |
50 | | - gs.info('Value "' + val + '" occurs ' + duplicates[val] + ' times.'); |
| 56 | + // --- (Optional) Retrieve Record Sys IDs for deeper analysis --- |
| 57 | + // Uncomment the section below if you need to list Sys IDs for each duplicate group |
| 58 | + /* |
| 59 | + duplicateGroups.forEach(function (dup) { |
| 60 | + gs.info('--- Records with "' + fieldName + '" = "' + dup.value + '" ---'); |
| 61 | + var dupRec = new GlideRecord(tableName); |
| 62 | + dupRec.addQuery(fieldName, dup.value); |
| 63 | + dupRec.query(); |
| 64 | + while (dupRec.next()) { |
| 65 | + gs.info(' Sys ID: ' + dupRec.getUniqueValue()); |
| 66 | + } |
| 67 | + }); |
| 68 | + */ |
51 | 69 | } |
52 | | -} |
| 70 | +})(); |
0 commit comments