|
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. |
| 1 | +# 🔍 Duplicate Record Finder (Server-Side Script) |
| 2 | + |
| 3 | +## 📁 Location |
| 4 | +**Category:** `Server-Side Components` |
| 5 | +**Subcategory:** `Background Scripts` |
| 6 | +**Snippet Folder:** `Duplicate Record Finder` |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## 📌 Description |
| 11 | + |
| 12 | +This server-side script helps identify **duplicate records** within any specified table and field in a ServiceNow instance. It uses the powerful `GlideAggregate` API to group and count entries, making it easy to detect data duplication issues across records. |
| 13 | + |
| 14 | +Designed to be executed via **Scripts - Background** or as a **Fix Script**, this utility provides fast insights into data quality without requiring complex queries or reports. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## 🚀 Features |
| 19 | + |
| 20 | +- ✅ Works on **any table** and **any field** |
| 21 | +- ✅ Uses `GlideAggregate` for efficient grouping and counting |
| 22 | +- ✅ Outputs a clear, readable summary of duplicate values |
| 23 | +- ✅ Helps detect issues like duplicate CI names, duplicate caller IDs, etc. |
| 24 | +- ✅ Non-destructive — the script does not modify any records |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## 📄 Script: `duplicate_finder.js` |
| 29 | + |
| 30 | +```javascript |
| 31 | +var tableName = 'incident'; // Change this to your table |
| 32 | +var fieldName = 'caller_id'; // Change this to your field |
| 33 | + |
| 34 | +if (!tableName || !fieldName) { |
| 35 | + gs.error('Table name and field name must be provided.'); |
| 36 | +} else { |
| 37 | + var ga = new GlideAggregate(tableName); |
| 38 | + ga.addAggregate('COUNT'); |
| 39 | + ga.groupBy(fieldName); |
| 40 | + ga.query(); |
| 41 | + |
| 42 | + var hasDuplicates = false; |
| 43 | + gs.print(`Duplicate values found in table: ${tableName}, field: ${fieldName}\n`); |
| 44 | + |
| 45 | + while (ga.next()) { |
| 46 | + var count = parseInt(ga.getAggregate('COUNT'), 10); |
| 47 | + if (count > 1) { |
| 48 | + hasDuplicates = true; |
| 49 | + gs.print(`Value: ${ga.getValue(fieldName)} | Count: ${count}`); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (!hasDuplicates) { |
| 54 | + gs.print('No duplicates found.'); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +🛠️ How to Use |
| 59 | + |
| 60 | +1) Navigate to System Definition > Scripts - Background |
| 61 | +2) Paste the script into the editor |
| 62 | +3) Update the tableName and fieldName variables |
| 63 | +4) Click Run Script |
| 64 | +5) Check the output for duplicate groups |
| 65 | + |
| 66 | +📸 Example Output |
| 67 | + |
| 68 | +Duplicate values found in table: incident, field: caller_id |
| 69 | + |
| 70 | +Value: 62826bf03710200044e0bfc8bcbe5df1 | Count: 4 |
| 71 | +Value: 681ccaf9c0a8016401c5a33be04be441 | Count: 2 |
| 72 | + |
| 73 | +Note: Values shown are backend values (e.g., sys_ids for reference fields) |
| 74 | + |
| 75 | +📂 File Structure |
| 76 | + |
| 77 | +Server-Side Components/ |
| 78 | +└── Background Scripts/ |
| 79 | + └── Duplicate Record Finder/ |
| 80 | + ├── README.md |
| 81 | + └── duplicate_finder.js |
| 82 | + |
| 83 | +⚙️ Requirements |
| 84 | + |
| 85 | +✅ Admin or script execution access |
| 86 | +✅ Valid tableName and fieldName |
| 87 | +🔁 Optional: Extend to resolve display values using GlideRecord if needed |
| 88 | + |
| 89 | +🧠 Use Case Examples |
| 90 | + |
| 91 | +1) Find duplicate caller_id values in the incident table |
| 92 | +2) Detect duplicated serial_number values in cmdb_ci_computer |
| 93 | +3) Validate unique constraints during data imports or migrations |
| 94 | + |
| 95 | +✅ Contribution Checklist Compliance |
| 96 | + |
| 97 | +✔️ Follows proper folder structure |
| 98 | +✔️ Contains a descriptive README.md |
| 99 | +✔️ Code is focused, relevant, and self-contained |
| 100 | +✔️ Does not include XML exports or sensitive data |
| 101 | +✔️ Uses ServiceNow-native APIs (GlideAggregate) |
| 102 | + |
| 103 | +👨💻 Author |
| 104 | + |
| 105 | +Contributor: @Shweyy123 |
| 106 | +Pull Request: #1846 |
| 107 | +Script Name: duplicate_finder.js |
| 108 | +Compatibility: Applicable to any ServiceNow version supporting GlideAggregate |
| 109 | + |
| 110 | +📘 License |
| 111 | + |
| 112 | +This script is open-source and provided for educational and development use. Always test in sub-production environments before applying to production data. |
| 113 | + |
| 114 | +🧩 Optional Enhancements |
| 115 | + |
| 116 | +1) Add logic to resolve display values from reference fields |
| 117 | +2) xtend output to a downloadable CSV format |
| 118 | +3) Turn into a Script Include or Scoped App utility |
27 | 119 |
|
0 commit comments