From 2a0f0ca167eefbb1b6e107dabdede854b1c64524 Mon Sep 17 00:00:00 2001 From: JohanDC-1999 <82451675+JohanDC-1999@users.noreply.github.com> Date: Sat, 11 Oct 2025 16:32:51 +0200 Subject: [PATCH 1/2] Update findDuplicatesByCombination.js to make the output more readable --- .../findDuplicatesByCombination.js | 62 +++++++++++++++---- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/Server-Side Components/Background Scripts/Duplicate Finder/findDuplicatesByCombination.js b/Server-Side Components/Background Scripts/Duplicate Finder/findDuplicatesByCombination.js index 21fb7b668e..83d3684999 100644 --- a/Server-Side Components/Background Scripts/Duplicate Finder/findDuplicatesByCombination.js +++ b/Server-Side Components/Background Scripts/Duplicate Finder/findDuplicatesByCombination.js @@ -1,7 +1,7 @@ /** -* Finds and reports records that have duplicate values across a combination of specified fields instead of a single field -* Useful for finding for example duplicate items where unique key is not just 1 field -*/ + * Finds and reports records that have duplicate values across a combination of specified fields instead of a single field + * Useful for finding for example duplicate items where unique key is not just 1 field + */ // --- UPDATE ONLY THE VALUES BELOW --- var tableName = 'cmdb_model'; // ADD: The table you want to check for duplicates. @@ -60,33 +60,73 @@ function findDuplicateCombinations(tableName, fieldNames) { while (duplicateAggregate.next()) { duplicateGroupCount++; var combinationDisplay = []; + var queryParams = []; for (var k = 0; k < fieldNames.length; k++) { var fieldName = fieldNames[k]; var fieldValue = duplicateAggregate.getDisplayValue(fieldName) || duplicateAggregate.getValue(fieldName); + var actualValue = duplicateAggregate.getValue(fieldName); combinationDisplay.push(fieldName + ': "' + fieldValue + '"'); + queryParams.push({ + field: fieldName, + value: actualValue + }); } var displayKey = combinationDisplay.join(', '); var countInGroup = duplicateAggregate.getAggregate('COUNT'); - duplicateJson[displayKey] = countInGroup; + duplicateJson[displayKey] = { + count: countInGroup, + queryParams: queryParams + }; } - /***************************************/ - /*** Print the Results ***/ - /***************************************/ + /***************************************************/ + /*** Print the Results & Fetch sys_id's ***/ + /***************************************************/ - // No duplicates found var fieldCombinationString = '"' + fieldNames.join('", "') + '"'; + if (Object.keys(duplicateJson).length === 0) { gs.print('No duplicates found for the field combination [' + fieldCombinationString + '] on table "' + tableName + '".'); return; } - // Duplicates were found - gs.print("Found " + duplicateGroupCount + " groups of duplicates based on the combination [" + fieldCombinationString + "]:"); + gs.print(''); + gs.print('======================================================'); + gs.print('DUPLICATE COMBINATIONS REPORT'); + gs.print('------------------------------------------------------'); + gs.print('Table: ' + tableName); + gs.print('Found ' + duplicateGroupCount + ' groups of duplicate records.'); + gs.print('======================================================\n'); + var groupNumber = 1; for (var key in duplicateJson) { - gs.print('Combination {' + key + '} has ' + duplicateJson[key] + ' occurrences.'); + var groupData = duplicateJson[key]; + + gs.print('Group ' + groupNumber + ':'); + gs.print(' - Occurrences: ' + groupData.count); + gs.print(' - Combination:'); + + var fields = key.split(', '); + for (var i = 0; i < fields.length; i++) { + gs.print(' - ' + fields[i]); + } + + gs.print(" - Duplicate sys_id's:"); + + // Perform the second query to get the sys_id's for this specific group + var rec = new GlideRecord(tableName); + for (var q = 0; q < groupData.queryParams.length; q++) { + var query = groupData.queryParams[q]; + rec.addQuery(query.field, query.value); + } + rec.query(); + + while (rec.next()) { + gs.print(' - ' + rec.getUniqueValue()); + } + gs.print(''); + groupNumber++; } } From ba55a7982c782cfcfa13f5aa8b8489047ee9a57d Mon Sep 17 00:00:00 2001 From: JohanDC-1999 <82451675+JohanDC-1999@users.noreply.github.com> Date: Sat, 11 Oct 2025 16:33:51 +0200 Subject: [PATCH 2/2] Update readme.md with the new output sample --- .../Background Scripts/Duplicate Finder/readme.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Server-Side Components/Background Scripts/Duplicate Finder/readme.md b/Server-Side Components/Background Scripts/Duplicate Finder/readme.md index 1f9a1931f2..5b047c6a65 100644 --- a/Server-Side Components/Background Scripts/Duplicate Finder/readme.md +++ b/Server-Side Components/Background Scripts/Duplicate Finder/readme.md @@ -45,3 +45,7 @@ For example, to find models with the same name, model number and manufacturer var tableName = 'cmdb_model'; var fieldNames = ['name', 'model_number', 'manufacturer']; ``` + +### Sample output +image +