Skip to content

Commit d49aa0f

Browse files
authored
Refactor Duplicate Records for any table script and Add Dynamic output support (#1947)
* Update Duplicate Records for any table based on field.js Enhancements Function name changed from DupCheck to findDuplicateRecords for clarity. Variable arr renamed to outputValues to better reflect its purpose. Variable gr renamed to aggregateGR for clearer context. Variable kb renamed to recordGR to remove table-specific implication. HTML entity > replaced with '>' in the addHaving clause. Use of addQuery('active', 'true') replaced with addActiveQuery() for standard filtering. Field access changed from direct property access (e.g. kb.number) to getValue() for consistency. Array logging improved by using JSON.stringify(outputValues) for readability. Hardcoded usage moved to a clearly marked section for easier customization. Added Functionality New parameter outputFields added to allow dynamic specification of which fields to include in the output. Each output entry is now a key-value object containing values from the specified outputFields. Logging per duplicate record now reflects the full output entry using JSON.stringify(outputEntry). * Update README.md Updated the Readme file to reflect enhancement to the script. In addition, made it clear that there are 2 scripts (which was already the case). And per script explained the functionality in more detail. Including that of the original single script.
1 parent a5bf3df commit d49aa0f

File tree

2 files changed

+71
-37
lines changed

2 files changed

+71
-37
lines changed
Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,53 @@
1-
// Function to check for duplicates in a specified field of a given table
2-
function DupCheck(table, field) {
3-
var arr = [];
4-
var gr = new GlideAggregate(table);
5-
6-
// Aggregate count of the specified field and group by it
7-
gr.addAggregate('COUNT', field);
8-
gr.groupBy(field);
9-
gr.addHaving('COUNT', '>', 1);
10-
gr.query();
11-
12-
gs.info("Please find the duplicates from the " + table + " for the field " + field + " below:");
13-
14-
// Loop through each group with duplicate counts
15-
while (gr.next()) {
16-
var duplicateValue = gr.getValue(field);
17-
var kb = new GlideRecord(table);
18-
19-
// Query for active records matching the duplicate value
20-
kb.addQuery(field, duplicateValue);
21-
kb.addQuery('active', 'true');
22-
kb.query();
23-
24-
// Collect and log the duplicates
25-
while (kb.next()) {
26-
arr.push(kb.sys_id.toString());
27-
gs.info('--> Number: {0}, and its Sys ID: {1}', [kb.number, kb.sys_id]);
1+
/**
2+
* Function to find duplicate records in any table based on a specified field.
3+
* Returns an array of values from specified output fields for records with duplicate field values.
4+
*
5+
* @param {string} tableName - Name of the table to search.
6+
* @param {string} fieldName - Field to check for duplicates.
7+
* @param {Array} outputFields - Array of field names to include in the output.
8+
*/
9+
function findDuplicateRecords(tableName, fieldName, outputFields) {
10+
var outputValues = [];
11+
12+
// Count occurrences of the field and group by it (fieldName)
13+
var aggregateGR = new GlideAggregate(tableName);
14+
aggregateGR.addAggregate('COUNT', fieldName);
15+
aggregateGR.groupBy(fieldName);
16+
aggregateGR.addHaving('COUNT', '>', 1); // Corrected HTML entity
17+
aggregateGR.query();
18+
19+
// Loop through each group with duplicate values
20+
while (aggregateGR.next()) {
21+
var duplicateValue = aggregateGR.getValue(fieldName);
22+
var recordGR = new GlideRecord(tableName);
23+
24+
// Query for records with the duplicate value
25+
recordGR.addQuery(fieldName, duplicateValue);
26+
recordGR.addActiveQuery(); // Standard active filter
27+
recordGR.query();
28+
29+
// Collect and log duplicate records
30+
while (recordGR.next()) {
31+
var outputEntry = {};
32+
for (var i = 0; i < outputFields.length; i++) {
33+
var field = outputFields[i];
34+
outputEntry[field] = recordGR.getValue(field);
35+
}
36+
outputValues.push(outputEntry);
37+
38+
//optional: log per duplicate record
39+
gs.info('--> Duplicate record: ' + JSON.stringify(outputEntry));
2840
}
2941
}
3042

31-
// Optionally return the array of sys_ids for further processing
32-
gs.info("array of sys_id's : " + arr);
33-
return arr;
43+
// Log the array of output values
44+
gs.info("Duplicate records: " + JSON.stringify(outputValues));
45+
46+
return outputValues;
3447
}
3548

36-
// Call the function to check for duplicates in the incident table
37-
DupCheck("kb_knowledge", "short_description");
49+
/**
50+
* === USAGE ===
51+
* Customize the table, field to check for duplicate values, and output fields below to run the duplicate check.
52+
*/
53+
findDuplicateRecords("kb_knowledge", "short_description", ["number", "sys_id", "short_description"]);
Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1-
This script helps to find out duplicate records in the table and returns an array of the duplicate records sys_id's.
1+
# Duplicate Records Scripts Overview
22

3-
In this example I have shown how to find out records in knowledge table.
3+
# Duplicate Records.js
44

5-
All you need to do is use the call the function with the table and field values as shown below:
6-
DupCheck("kb_knowledge", "short_description");
7-
where "DupCheck" is the function, "kb_knowledge" is the table name and "short_description" is the field based on which your duplicates will be found.
5+
This script identifies duplicate values in a specific field of a fixed table (`incident`) and logs how many times each duplicate occurs.
6+
7+
- Uses `GlideAggregate` to count and group by the `number` field.
8+
- Logs each duplicate record directly to the console.
9+
- Limited to the `incident` table and `number` field.
10+
- No output array is returned; results are only printed.
11+
12+
---
13+
14+
# Duplicate Records for any table based on field.js
15+
16+
This script finds duplicate records in **any table** based on a specified field and returns an array of values from fields you choose.
17+
18+
- Uses `GlideAggregate` to detect duplicates and `GlideRecord` to retrieve full record details.
19+
- Function name: `findDuplicateRecords`
20+
- Accepts three parameters:
21+
- `tableName`: the table to search
22+
- `fieldName`: the field to check for duplicates
23+
- `outputFields`: an array of field names to include in the output
24+
- Logs each duplicate record as a structured JSON object.
25+
- Returns a readable array of objects containing the specified output fields.

0 commit comments

Comments
 (0)