|
1 | 1 | /** |
2 | | -* Finds and reports records that have duplicate values across a combination of specified fields instead of a single field |
3 | | -* Useful for finding for example duplicate items where unique key is not just 1 field |
4 | | -*/ |
| 2 | + * Finds and reports records that have duplicate values across a combination of specified fields instead of a single field |
| 3 | + * Useful for finding for example duplicate items where unique key is not just 1 field |
| 4 | + */ |
5 | 5 |
|
6 | 6 | // --- UPDATE ONLY THE VALUES BELOW --- |
7 | 7 | var tableName = 'cmdb_model'; // ADD: The table you want to check for duplicates. |
@@ -60,33 +60,73 @@ function findDuplicateCombinations(tableName, fieldNames) { |
60 | 60 | while (duplicateAggregate.next()) { |
61 | 61 | duplicateGroupCount++; |
62 | 62 | var combinationDisplay = []; |
| 63 | + var queryParams = []; |
63 | 64 |
|
64 | 65 | for (var k = 0; k < fieldNames.length; k++) { |
65 | 66 | var fieldName = fieldNames[k]; |
66 | 67 | var fieldValue = duplicateAggregate.getDisplayValue(fieldName) || duplicateAggregate.getValue(fieldName); |
| 68 | + var actualValue = duplicateAggregate.getValue(fieldName); |
67 | 69 | combinationDisplay.push(fieldName + ': "' + fieldValue + '"'); |
| 70 | + queryParams.push({ |
| 71 | + field: fieldName, |
| 72 | + value: actualValue |
| 73 | + }); |
68 | 74 | } |
69 | 75 |
|
70 | 76 | var displayKey = combinationDisplay.join(', '); |
71 | 77 | var countInGroup = duplicateAggregate.getAggregate('COUNT'); |
72 | | - duplicateJson[displayKey] = countInGroup; |
| 78 | + duplicateJson[displayKey] = { |
| 79 | + count: countInGroup, |
| 80 | + queryParams: queryParams |
| 81 | + }; |
73 | 82 | } |
74 | 83 |
|
75 | | - /***************************************/ |
76 | | - /*** Print the Results ***/ |
77 | | - /***************************************/ |
| 84 | + /***************************************************/ |
| 85 | + /*** Print the Results & Fetch sys_id's ***/ |
| 86 | + /***************************************************/ |
78 | 87 |
|
79 | | - // No duplicates found |
80 | 88 | var fieldCombinationString = '"' + fieldNames.join('", "') + '"'; |
| 89 | + |
81 | 90 | if (Object.keys(duplicateJson).length === 0) { |
82 | 91 | gs.print('No duplicates found for the field combination [' + fieldCombinationString + '] on table "' + tableName + '".'); |
83 | 92 | return; |
84 | 93 | } |
85 | 94 |
|
86 | | - // Duplicates were found |
87 | | - gs.print("Found " + duplicateGroupCount + " groups of duplicates based on the combination [" + fieldCombinationString + "]:"); |
| 95 | + gs.print(''); |
| 96 | + gs.print('======================================================'); |
| 97 | + gs.print('DUPLICATE COMBINATIONS REPORT'); |
| 98 | + gs.print('------------------------------------------------------'); |
| 99 | + gs.print('Table: ' + tableName); |
| 100 | + gs.print('Found ' + duplicateGroupCount + ' groups of duplicate records.'); |
| 101 | + gs.print('======================================================\n'); |
88 | 102 |
|
| 103 | + var groupNumber = 1; |
89 | 104 | for (var key in duplicateJson) { |
90 | | - gs.print('Combination {' + key + '} has ' + duplicateJson[key] + ' occurrences.'); |
| 105 | + var groupData = duplicateJson[key]; |
| 106 | + |
| 107 | + gs.print('Group ' + groupNumber + ':'); |
| 108 | + gs.print(' - Occurrences: ' + groupData.count); |
| 109 | + gs.print(' - Combination:'); |
| 110 | + |
| 111 | + var fields = key.split(', '); |
| 112 | + for (var i = 0; i < fields.length; i++) { |
| 113 | + gs.print(' - ' + fields[i]); |
| 114 | + } |
| 115 | + |
| 116 | + gs.print(" - Duplicate sys_id's:"); |
| 117 | + |
| 118 | + // Perform the second query to get the sys_id's for this specific group |
| 119 | + var rec = new GlideRecord(tableName); |
| 120 | + for (var q = 0; q < groupData.queryParams.length; q++) { |
| 121 | + var query = groupData.queryParams[q]; |
| 122 | + rec.addQuery(query.field, query.value); |
| 123 | + } |
| 124 | + rec.query(); |
| 125 | + |
| 126 | + while (rec.next()) { |
| 127 | + gs.print(' - ' + rec.getUniqueValue()); |
| 128 | + } |
| 129 | + gs.print(''); |
| 130 | + groupNumber++; |
91 | 131 | } |
92 | 132 | } |
0 commit comments