diff --git a/Server-Side Components/Background Scripts/Duplicate Finder/readme.md b/Server-Side Components/Background Scripts/Duplicate Finder/readme.md new file mode 100644 index 0000000000..3ca613a445 --- /dev/null +++ b/Server-Side Components/Background Scripts/Duplicate Finder/readme.md @@ -0,0 +1,27 @@ +## ServiceNow Duplicate Record Finder +A simple server-side script for ServiceNow that finds and reports on duplicate values for any field on any table. It uses an efficient GlideAggregate query and formats the results into a clean, readable report. + +### How to Use +This script is designed to be run in **Scripts - Background** or as a Fix Script. +1. Navigate: Go to **System Definition > Scripts - Background** (or type sys.scripts.do in the filter navigator). +2. Copy Script: Copy the entire contents of the script.js file. +3. Paste and Configure: Paste the script into the "Run script" text box. Add the table to search in `tableName` and the field to search for duplicates in `fieldName` + ```js + // Update ONLY below values to find duplicates + var tableName = ''; // ADD: Table you want for duplicates + var fieldName = 'field_name'; // ADD: Field that you want to check for duplicates + ``` + For example, to find duplicate email addresses in the User table: + ```js + var tableName = 'sys_user'; + var fieldName = 'email';; + ``` + To find incidents with the same short description: + ```js + var tableName = 'incident'; + var fieldName = 'short_description'; + ``` + + +4. Run Script: Click the "Run script" button. The results will be displayed on the screen below the editor. + diff --git a/Server-Side Components/Background Scripts/Duplicate Finder/script.js b/Server-Side Components/Background Scripts/Duplicate Finder/script.js new file mode 100644 index 0000000000..3318e5faa0 --- /dev/null +++ b/Server-Side Components/Background Scripts/Duplicate Finder/script.js @@ -0,0 +1,64 @@ +// Update ONLY below values to find duplicates +var tableName = 'incident'; // ADD: Table you want for duplicates +var fieldName = 'short_description'; // ADD: Field that you want to check for duplicates + +findDuplicates(tableName, fieldName); + +function findDuplicates(tableName, fieldName) { + /**************************************/ + /*** Basic error handling on inputs ***/ + /**************************************/ + + // Check if table exists + if (!gs.tableExists(tableName)) { + // MODIFIED: Switched to string concatenation + gs.info('Table "' + tableName + '" does not exist.'); + return; + } + + // Check if field exists + var gr = new GlideRecord(tableName); + gr.initialize(); + if (!gr.isValidField(fieldName)) { + gs.print('No field called "' + fieldName + '" on the "' + tableName + '" table.'); + return; + } + + /***************************************/ + /*********** Find duplicates ***********/ + /***************************************/ + var duplicateJson = {}; // Store the duplicate records + var duplicateGroupCount = 0; // Counts the number of groups of duplicates + + var duplicateAggregate = new GlideAggregate(tableName); + duplicateAggregate.addAggregate('COUNT', fieldName); + duplicateAggregate.groupBy(fieldName); + duplicateAggregate.addHaving('COUNT', '>', 1); // More than 1 means it is a duplicate + duplicateAggregate.addNotNullQuery(fieldName); // Ignore records where the field is empty + duplicateAggregate.query(); + + while (duplicateAggregate.next()) { + duplicateGroupCount++; + var fieldValue = duplicateAggregate.getValue(fieldName); + var countInGroup = duplicateAggregate.getAggregate('COUNT', fieldName); + duplicateJson[fieldValue] = countInGroup; + } + + /***************************************/ + /********** Print the results **********/ + /***************************************/ + + // No duplicates found + if (Object.keys(duplicateJson).length === 0) { + gs.print('No duplicates found for field "' + fieldName + '" on table "' + tableName + '".'); + return; + } + + // Duplicates were found + gs.print("Found " + duplicateGroupCount + " groups of duplicates:"); + + for (var key in duplicateJson) { + gs.print('Value "' + key + '" has ' + duplicateJson[key] + ' occurrences.'); + } +} +