|
1 | | -// Update ONLY below values to find duplicates |
2 | | -var tableName = 'incident'; // ADD: Table you want for duplicates |
3 | | -var fieldName = 'short_description'; // ADD: Field that you want to check for duplicates |
| 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 | + |
| 5 | +var tableName = 'incident'; // Set your target table here |
| 6 | +var fieldName = 'short_description'; // Set the target field to check duplicates |
4 | 7 |
|
5 | 8 | findDuplicates(tableName, fieldName); |
6 | 9 |
|
7 | 10 | function findDuplicates(tableName, fieldName) { |
8 | | - /**************************************/ |
9 | | - /*** Basic error handling on inputs ***/ |
10 | | - /**************************************/ |
11 | | - |
12 | | - // Check if table exists |
| 11 | + // Validate that the table exists |
13 | 12 | if (!gs.tableExists(tableName)) { |
14 | | - // MODIFIED: Switched to string concatenation |
15 | 13 | gs.info('Table "' + tableName + '" does not exist.'); |
16 | 14 | return; |
17 | 15 | } |
18 | 16 |
|
19 | | - // Check if field exists |
| 17 | + // Validate that the field exists on the table |
20 | 18 | var gr = new GlideRecord(tableName); |
21 | 19 | gr.initialize(); |
22 | 20 | if (!gr.isValidField(fieldName)) { |
23 | | - gs.print('No field called "' + fieldName + '" on the "' + tableName + '" table.'); |
| 21 | + gs.info('Field "' + fieldName + '" does not exist on table "' + tableName + '".'); |
24 | 22 | return; |
25 | 23 | } |
26 | 24 |
|
27 | | - /***************************************/ |
28 | | - /*********** Find duplicates ***********/ |
29 | | - /***************************************/ |
30 | | - var duplicateJson = {}; // Store the duplicate records |
31 | | - var duplicateGroupCount = 0; // Counts the number of groups of duplicates |
32 | | - |
33 | | - var duplicateAggregate = new GlideAggregate(tableName); |
34 | | - duplicateAggregate.addAggregate('COUNT', fieldName); |
35 | | - duplicateAggregate.groupBy(fieldName); |
36 | | - duplicateAggregate.addHaving('COUNT', '>', 1); // More than 1 means it is a duplicate |
37 | | - duplicateAggregate.addNotNullQuery(fieldName); // Ignore records where the field is empty |
38 | | - duplicateAggregate.query(); |
39 | | - |
40 | | - while (duplicateAggregate.next()) { |
41 | | - duplicateGroupCount++; |
42 | | - var fieldValue = duplicateAggregate.getValue(fieldName); |
43 | | - var countInGroup = duplicateAggregate.getAggregate('COUNT', fieldName); |
44 | | - duplicateJson[fieldValue] = countInGroup; |
| 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++; |
45 | 41 | } |
46 | 42 |
|
47 | | - /***************************************/ |
48 | | - /********** Print the results **********/ |
49 | | - /***************************************/ |
50 | | - |
51 | | - // No duplicates found |
52 | | - if (Object.keys(duplicateJson).length === 0) { |
53 | | - gs.print('No duplicates found for field "' + fieldName + '" on table "' + tableName + '".'); |
| 43 | + if (duplicateCount === 0) { |
| 44 | + gs.info('No duplicates found for field "' + fieldName + '" on table "' + tableName + '".'); |
54 | 45 | return; |
55 | 46 | } |
56 | 47 |
|
57 | | - // Duplicates were found |
58 | | - gs.print("Found " + duplicateGroupCount + " groups of duplicates:"); |
59 | | - |
60 | | - for (var key in duplicateJson) { |
61 | | - gs.print('Value "' + key + '" has ' + duplicateJson[key] + ' occurrences.'); |
| 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.'); |
62 | 51 | } |
63 | 52 | } |
64 | | - |
0 commit comments