diff --git a/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/Backgroundscript_clientscript_duplicate_1.png b/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/Backgroundscript_clientscript_duplicate_1.png new file mode 100644 index 0000000000..87d22ca1b5 Binary files /dev/null and b/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/Backgroundscript_clientscript_duplicate_1.png differ diff --git a/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/Backgroundscript_clientscript_duplicate_3.png b/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/Backgroundscript_clientscript_duplicate_3.png new file mode 100644 index 0000000000..2855a28db0 Binary files /dev/null and b/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/Backgroundscript_clientscript_duplicate_3.png differ diff --git a/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/Backgroundscript_clientscript_duplicate_4.png b/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/Backgroundscript_clientscript_duplicate_4.png new file mode 100644 index 0000000000..e436ab0e50 Binary files /dev/null and b/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/Backgroundscript_clientscript_duplicate_4.png differ diff --git a/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/README.md b/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/README.md new file mode 100644 index 0000000000..fae96a687c --- /dev/null +++ b/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/README.md @@ -0,0 +1,64 @@ +# Duplicate Client Script Audit for Table + +Detects duplicate Client Scripts created on a specific table such as Incident, Change Request, or Problem. Helps identify redundant client scripts that share the same configuration. + +## Use Case + +Every ServiceNow developer Junior or senior has been there, you're building something late at night, testing a client script, and you hit **“Insert and Stay”** instead of **“Update.”** It looks harmless at first until a few days later, your form starts behaving strangely because multiple client scripts with the same logic are firing at once. +This script was created for exactly that situation: +- It helps you **find and report duplicate client scripts** on any table, so you can clean them up before they cause issues. +- It scans through active client scripts and highlights cases where multiple scripts: + - Have the same **name**, **UI type**, **type**, **order**, and **field** (for *onChange* scripts) + - Exist within the same table + - Are still **active** in the system + - By running this audit, you can quickly detect redundant client scripts that may have been unintentionally cloned + + +### Functionality +Uses a Background Script to: +- Query all active client scripts for the selected table +- Sort them by creation date (oldest to newest) +- Detect duplicates with identical configurations +- Display results as: + - [ORIGINAL] – first created client script + - This script has X duplicates – count of newer scripts with the same trigger setup +- If no duplicates are found, a message confirms: + - No duplicate client scripts found for this table + +### Steps to Run + +1. Navigate to **System Definition → Scripts - Background** +2. Click **New** +3. Paste the provided script and modify this line as needed: + - var targetTable = 'incident'; // or 'change_request', 'wm_order' etc.... +4. Run Script + +--- + +### Example:1 Run The Bacground Script For Change Request Table + +![Duplicate Client Script Audit Output](Backgroundscript_clientscript_duplicate_4.png) + +--- + +### Cross Checking The result from output of Script Run + +![Duplicate Client Script Audit Output](Backgroundscript_clientscript_duplicate_1.png) + +--- + +### Example:2 Run The Bacground Script For Incident Table + +![Duplicate Client Script Audit Output](Backgroundscript_clientscript_duplicate_3.png) + + + + + + + + + + + + diff --git a/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/backGroundScriptDuplicateClientScript.js b/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/backGroundScriptDuplicateClientScript.js new file mode 100644 index 0000000000..a3755fd765 --- /dev/null +++ b/Server-Side Components/Background Scripts/Duplicate Client Script Audit for Tables/backGroundScriptDuplicateClientScript.js @@ -0,0 +1,83 @@ +// Target table +var targetTable = 'change_request'; // < can be incident, change_request , wm_order etc.... > + +// counters +var seen = {}; +var duplicateCount = 0; +var totalCount = 0; + +// Query ACTIVE Client Scripts sorted by creation date (oldest → newest) +var gr = new GlideRecord('sys_script_client'); +gr.addQuery('table', targetTable); +gr.addQuery('active', true); +gr.orderBy('sys_created_on'); +gr.query(); + +gs.print('--- Duplicate Client Script Audit for table: ' + targetTable + ' ---'); +gs.print('Sorted by creation date (oldest → newest)'); +gs.print('MODE: Detection only (no updates performed).'); +gs.print(''); + +// group Client Scripts +while (gr.next()) { + totalCount++; + + // Build unique trigger key + var key = gr.name + '_' + gr.ui_type + '_' + gr.type + '_' + gr.order; + if (gr.type == 'onChange') key += '_' + gr.field; + + // Build readable script info + var info = gr.name + + ' | Type: ' + gr.type + + (gr.type == 'onChange' ? ' | Field: ' + gr.field : '') + + ' | Order: ' + gr.order + + ' | Created: ' + gr.sys_created_on.getDisplayValue() + + ' | Sys_id: (' + gr.sys_id + ')'; + + + // If first occurrence consider it as key + if (!seen[key]) { + seen[key] = { + original: info, + duplicates: [] + }; + } + // If key already exists put into duplicate + else { + seen[key].duplicates.push(info); + duplicateCount++; + } +} + +// grouping the scripts +var groupsWithDuplicates = 0; +for (var key in seen) { + var group = seen[key]; + if (group.duplicates.length > 0) { + groupsWithDuplicates++; + + // Original + gs.print(group.original); + + // Summary line + // gs.print('This ' + seen.key + 'script has ' + group.duplicates.length + ' duplicate' ); + var trimmedKey = key.split('_')[0]; + gs.print('This ' + trimmedKey + ' script has ' + group.duplicates.length + ' duplicate' + (group.duplicates.length > 1 ? 's.' : '.')); + + // Separator + gs.print('--------------------------------------'); + } +} + +// no duplicates found +if (groupsWithDuplicates === 0) { + gs.print('No duplicate client scripts found for this table.'); +} + +// ✅ Final summary +gs.print('\n--------------------------------------'); +gs.print('✅ SUMMARY for table: ' + targetTable); +gs.print('Total active scripts scanned: ' + totalCount); +gs.print('Originals with duplicates: ' + groupsWithDuplicates); +gs.print('Total duplicates detected: ' + duplicateCount); +gs.print('--------------------------------------');