Skip to content

Commit a78604c

Browse files
author
SWETHA R
authored
Update readme.md
1 parent 6ce8200 commit a78604c

File tree

1 file changed

+33
-31
lines changed
  • Server-Side Components/Background Scripts/Duplicate Finder

1 file changed

+33
-31
lines changed

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

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,43 @@ Designed to be executed via **Scripts - Background** or as a **Fix Script**, thi
3131
var tableName = 'incident'; // Change this to your table
3232
var fieldName = 'caller_id'; // Change this to your field
3333

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-
}
34+
// --- Validation ---
35+
if (!gs.tableExists(tableName)) {
36+
gs.error('❌ Table "' + tableName + '" does not exist.');
37+
return;
38+
}
39+
40+
var gr = new GlideRecord(tableName);
41+
gr.initialize();
42+
if (!gr.isValidField(fieldName)) {
43+
gs.error('❌ Field "' + fieldName + '" does not exist on table "' + tableName + '".');
44+
return;
45+
}
5246

53-
if (!hasDuplicates) {
54-
gs.print('No duplicates found.');
47+
// --- Find Duplicates ---
48+
var ga = new GlideAggregate(tableName);
49+
ga.addAggregate('COUNT', fieldName);
50+
ga.groupBy(fieldName);
51+
ga.addHaving('COUNT', '>', 1);
52+
ga.addNotNullQuery(fieldName);
53+
ga.query();
54+
55+
var hasDuplicates = false;
56+
gs.info('🔍 Checking duplicates in table: ' + tableName + ', field: ' + fieldName);
57+
58+
while (ga.next()) {
59+
var count = parseInt(ga.getAggregate('COUNT', fieldName), 10);
60+
if (count > 1) {
61+
hasDuplicates = true;
62+
gs.info('⚠️ Value: ' + ga.getDisplayValue(fieldName) + ' | Count: ' + count);
5563
}
5664
}
5765

66+
if (!hasDuplicates) {
67+
gs.info('✅ No duplicates found for "' + fieldName + '" on "' + tableName + '".');
68+
}
69+
70+
5871
🛠️ How to Use
5972

6073
1) Navigate to System Definition > Scripts - Background
@@ -100,17 +113,6 @@ Server-Side Components/
100113
✔️ Does not include XML exports or sensitive data
101114
✔️ Uses ServiceNow-native APIs (GlideAggregate)
102115

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-
114116
🧩 Optional Enhancements
115117

116118
1) Add logic to resolve display values from reference fields

0 commit comments

Comments
 (0)