From 1cbdd762a72c46e70ab351dff913592f493ca751 Mon Sep 17 00:00:00 2001 From: Willem Date: Wed, 8 Oct 2025 13:34:46 +0200 Subject: [PATCH 1/2] 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). --- ...te Records for any table based on field.js | 80 +++++++++++-------- 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/Server-Side Components/Background Scripts/Find out Duplicate Records/Duplicate Records for any table based on field.js b/Server-Side Components/Background Scripts/Find out Duplicate Records/Duplicate Records for any table based on field.js index 42b71aaee2..a32469d2a0 100644 --- a/Server-Side Components/Background Scripts/Find out Duplicate Records/Duplicate Records for any table based on field.js +++ b/Server-Side Components/Background Scripts/Find out Duplicate Records/Duplicate Records for any table based on field.js @@ -1,37 +1,53 @@ -// Function to check for duplicates in a specified field of a given table -function DupCheck(table, field) { - var arr = []; - var gr = new GlideAggregate(table); - - // Aggregate count of the specified field and group by it - gr.addAggregate('COUNT', field); - gr.groupBy(field); - gr.addHaving('COUNT', '>', 1); - gr.query(); - - gs.info("Please find the duplicates from the " + table + " for the field " + field + " below:"); - - // Loop through each group with duplicate counts - while (gr.next()) { - var duplicateValue = gr.getValue(field); - var kb = new GlideRecord(table); - - // Query for active records matching the duplicate value - kb.addQuery(field, duplicateValue); - kb.addQuery('active', 'true'); - kb.query(); - - // Collect and log the duplicates - while (kb.next()) { - arr.push(kb.sys_id.toString()); - gs.info('--> Number: {0}, and its Sys ID: {1}', [kb.number, kb.sys_id]); +/** + * Function to find duplicate records in any table based on a specified field. + * Returns an array of values from specified output fields for records with duplicate field values. + * + * @param {string} tableName - Name of the table to search. + * @param {string} fieldName - Field to check for duplicates. + * @param {Array} outputFields - Array of field names to include in the output. + */ +function findDuplicateRecords(tableName, fieldName, outputFields) { + var outputValues = []; + + // Count occurrences of the field and group by it (fieldName) + var aggregateGR = new GlideAggregate(tableName); + aggregateGR.addAggregate('COUNT', fieldName); + aggregateGR.groupBy(fieldName); + aggregateGR.addHaving('COUNT', '>', 1); // Corrected HTML entity + aggregateGR.query(); + + // Loop through each group with duplicate values + while (aggregateGR.next()) { + var duplicateValue = aggregateGR.getValue(fieldName); + var recordGR = new GlideRecord(tableName); + + // Query for records with the duplicate value + recordGR.addQuery(fieldName, duplicateValue); + recordGR.addActiveQuery(); // Standard active filter + recordGR.query(); + + // Collect and log duplicate records + while (recordGR.next()) { + var outputEntry = {}; + for (var i = 0; i < outputFields.length; i++) { + var field = outputFields[i]; + outputEntry[field] = recordGR.getValue(field); + } + outputValues.push(outputEntry); + + //optional: log per duplicate record + gs.info('--> Duplicate record: ' + JSON.stringify(outputEntry)); } } - // Optionally return the array of sys_ids for further processing - gs.info("array of sys_id's : " + arr); - return arr; + // Log the array of output values + gs.info("Duplicate records: " + JSON.stringify(outputValues)); + + return outputValues; } -// Call the function to check for duplicates in the incident table -DupCheck("kb_knowledge", "short_description"); \ No newline at end of file +/** + * === USAGE === + * Customize the table, field to check for duplicate values, and output fields below to run the duplicate check. + */ +findDuplicateRecords("kb_knowledge", "short_description", ["number", "sys_id", "short_description"]); From 05040271e7666894c8118175c1b68da432080b3d Mon Sep 17 00:00:00 2001 From: Willem Date: Wed, 8 Oct 2025 13:44:59 +0200 Subject: [PATCH 2/2] 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. --- .../Find out Duplicate Records/README.md | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/Server-Side Components/Background Scripts/Find out Duplicate Records/README.md b/Server-Side Components/Background Scripts/Find out Duplicate Records/README.md index 9c2a116fc1..e9fa3c41e7 100644 --- a/Server-Side Components/Background Scripts/Find out Duplicate Records/README.md +++ b/Server-Side Components/Background Scripts/Find out Duplicate Records/README.md @@ -1,7 +1,25 @@ -This script helps to find out duplicate records in the table and returns an array of the duplicate records sys_id's. +# Duplicate Records Scripts Overview -In this example I have shown how to find out records in knowledge table. +# Duplicate Records.js -All you need to do is use the call the function with the table and field values as shown below: -DupCheck("kb_knowledge", "short_description"); -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. \ No newline at end of file +This script identifies duplicate values in a specific field of a fixed table (`incident`) and logs how many times each duplicate occurs. + +- Uses `GlideAggregate` to count and group by the `number` field. +- Logs each duplicate record directly to the console. +- Limited to the `incident` table and `number` field. +- No output array is returned; results are only printed. + +--- + +# Duplicate Records for any table based on field.js + +This script finds duplicate records in **any table** based on a specified field and returns an array of values from fields you choose. + +- Uses `GlideAggregate` to detect duplicates and `GlideRecord` to retrieve full record details. +- Function name: `findDuplicateRecords` +- Accepts three parameters: + - `tableName`: the table to search + - `fieldName`: the field to check for duplicates + - `outputFields`: an array of field names to include in the output +- Logs each duplicate record as a structured JSON object. +- Returns a readable array of objects containing the specified output fields.