Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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)












Original file line number Diff line number Diff line change
@@ -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('--------------------------------------');
Loading