Skip to content

Commit ee96566

Browse files
authored
feat(background-script): Overhaul duplicate report output for clarity and actionability (#2034)
* Update findDuplicatesByCombination.js to make the output more readable * Update readme.md with the new output sample
1 parent 8ccf794 commit ee96566

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

Server-Side Components/Background Scripts/Duplicate Finder/findDuplicatesByCombination.js

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
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+
*/
55

66
// --- UPDATE ONLY THE VALUES BELOW ---
77
var tableName = 'cmdb_model'; // ADD: The table you want to check for duplicates.
@@ -60,33 +60,73 @@ function findDuplicateCombinations(tableName, fieldNames) {
6060
while (duplicateAggregate.next()) {
6161
duplicateGroupCount++;
6262
var combinationDisplay = [];
63+
var queryParams = [];
6364

6465
for (var k = 0; k < fieldNames.length; k++) {
6566
var fieldName = fieldNames[k];
6667
var fieldValue = duplicateAggregate.getDisplayValue(fieldName) || duplicateAggregate.getValue(fieldName);
68+
var actualValue = duplicateAggregate.getValue(fieldName);
6769
combinationDisplay.push(fieldName + ': "' + fieldValue + '"');
70+
queryParams.push({
71+
field: fieldName,
72+
value: actualValue
73+
});
6874
}
6975

7076
var displayKey = combinationDisplay.join(', ');
7177
var countInGroup = duplicateAggregate.getAggregate('COUNT');
72-
duplicateJson[displayKey] = countInGroup;
78+
duplicateJson[displayKey] = {
79+
count: countInGroup,
80+
queryParams: queryParams
81+
};
7382
}
7483

75-
/***************************************/
76-
/*** Print the Results ***/
77-
/***************************************/
84+
/***************************************************/
85+
/*** Print the Results & Fetch sys_id's ***/
86+
/***************************************************/
7887

79-
// No duplicates found
8088
var fieldCombinationString = '"' + fieldNames.join('", "') + '"';
89+
8190
if (Object.keys(duplicateJson).length === 0) {
8291
gs.print('No duplicates found for the field combination [' + fieldCombinationString + '] on table "' + tableName + '".');
8392
return;
8493
}
8594

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');
88102

103+
var groupNumber = 1;
89104
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++;
91131
}
92132
}

Server-Side Components/Background Scripts/Duplicate Finder/readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ For example, to find models with the same name, model number and manufacturer
4545
var tableName = 'cmdb_model';
4646
var fieldNames = ['name', 'model_number', 'manufacturer'];
4747
```
48+
49+
### Sample output
50+
<img width="518" height="483" alt="image" src="https://github.com/user-attachments/assets/b5508bd8-4a3d-4478-95bd-fc6f770d4de2" />
51+

0 commit comments

Comments
 (0)