Skip to content

Commit da74be6

Browse files
authored
feat: Add duplicate finder script to find duplicates on any table (#1781)
* Create readme.md * Create script.js which finds duplicates * Update readme.md * Update script.js * Update readme.md * Update script.js to work on inherited fields
1 parent d2bb5ce commit da74be6

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## ServiceNow Duplicate Record Finder
2+
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.
3+
4+
### How to Use
5+
This script is designed to be run in **Scripts - Background** or as a Fix Script.
6+
1. Navigate: Go to **System Definition > Scripts - Background** (or type sys.scripts.do in the filter navigator).
7+
2. Copy Script: Copy the entire contents of the script.js file.
8+
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`
9+
```js
10+
// Update ONLY below values to find duplicates
11+
var tableName = '<table_name>'; // ADD: Table you want for duplicates
12+
var fieldName = 'field_name'; // ADD: Field that you want to check for duplicates
13+
```
14+
For example, to find duplicate email addresses in the User table:
15+
```js
16+
var tableName = 'sys_user';
17+
var fieldName = 'email';;
18+
```
19+
To find incidents with the same short description:
20+
```js
21+
var tableName = 'incident';
22+
var fieldName = 'short_description';
23+
```
24+
25+
26+
4. Run Script: Click the "Run script" button. The results will be displayed on the screen below the editor.
27+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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
4+
5+
findDuplicates(tableName, fieldName);
6+
7+
function findDuplicates(tableName, fieldName) {
8+
/**************************************/
9+
/*** Basic error handling on inputs ***/
10+
/**************************************/
11+
12+
// Check if table exists
13+
if (!gs.tableExists(tableName)) {
14+
// MODIFIED: Switched to string concatenation
15+
gs.info('Table "' + tableName + '" does not exist.');
16+
return;
17+
}
18+
19+
// Check if field exists
20+
var gr = new GlideRecord(tableName);
21+
gr.initialize();
22+
if (!gr.isValidField(fieldName)) {
23+
gs.print('No field called "' + fieldName + '" on the "' + tableName + '" table.');
24+
return;
25+
}
26+
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;
45+
}
46+
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 + '".');
54+
return;
55+
}
56+
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.');
62+
}
63+
}
64+

0 commit comments

Comments
 (0)