Skip to content

Commit 278f672

Browse files
author
SWETHA R
authored
Updated script.js
1 parent 9a8fe4f commit 278f672

File tree

1 file changed

+30
-42
lines changed
  • Server-Side Components/Background Scripts/Duplicate Finder

1 file changed

+30
-42
lines changed
Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,52 @@
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
1+
// Duplicate Record Finder
2+
// Usage: Run in Scripts - Background or as a Fix Script
3+
// Update the variables 'tableName' and 'fieldName' below before running
4+
5+
var tableName = 'incident'; // Set your target table here
6+
var fieldName = 'short_description'; // Set the target field to check duplicates
47

58
findDuplicates(tableName, fieldName);
69

710
function findDuplicates(tableName, fieldName) {
8-
/**************************************/
9-
/*** Basic error handling on inputs ***/
10-
/**************************************/
11-
12-
// Check if table exists
11+
// Validate that the table exists
1312
if (!gs.tableExists(tableName)) {
14-
// MODIFIED: Switched to string concatenation
1513
gs.info('Table "' + tableName + '" does not exist.');
1614
return;
1715
}
1816

19-
// Check if field exists
17+
// Validate that the field exists on the table
2018
var gr = new GlideRecord(tableName);
2119
gr.initialize();
2220
if (!gr.isValidField(fieldName)) {
23-
gs.print('No field called "' + fieldName + '" on the "' + tableName + '" table.');
21+
gs.info('Field "' + fieldName + '" does not exist on table "' + tableName + '".');
2422
return;
2523
}
2624

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;
25+
// Prepare GlideAggregate to find duplicates
26+
var ga = new GlideAggregate(tableName);
27+
ga.addAggregate('COUNT', fieldName);
28+
ga.groupBy(fieldName);
29+
ga.addHaving('COUNT', '>', 1); // More than 1 means duplicates exist
30+
ga.addNotNullQuery(fieldName); // Ignore null or empty values
31+
ga.query();
32+
33+
var duplicateCount = 0;
34+
var duplicates = {};
35+
36+
while (ga.next()) {
37+
var value = ga.getValue(fieldName);
38+
var count = parseInt(ga.getAggregate('COUNT', fieldName), 10);
39+
duplicates[value] = count;
40+
duplicateCount++;
4541
}
4642

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 + '".');
43+
if (duplicateCount === 0) {
44+
gs.info('No duplicates found for field "' + fieldName + '" on table "' + tableName + '".');
5445
return;
5546
}
5647

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.');
48+
gs.info('Found ' + duplicateCount + ' groups of duplicates for field "' + fieldName + '" on table "' + tableName + '":');
49+
for (var val in duplicates) {
50+
gs.info('Value "' + val + '" occurs ' + duplicates[val] + ' times.');
6251
}
6352
}
64-

0 commit comments

Comments
 (0)