Skip to content

Commit 3ba5909

Browse files
authored
Duplicate Client Script Audit for Table (#2521)
* Create README.md * Add files via upload * Update README.md updates wording on readme file * Create backGroundScriptDuplicateClientScript.js
1 parent 41877af commit 3ba5909

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed
Loading
32.4 KB
Loading
49.6 KB
Loading
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Duplicate Client Script Audit for Table
2+
3+
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.
4+
5+
## Use Case
6+
7+
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.
8+
This script was created for exactly that situation:
9+
- It helps you **find and report duplicate client scripts** on any table, so you can clean them up before they cause issues.
10+
- It scans through active client scripts and highlights cases where multiple scripts:
11+
- Have the same **name**, **UI type**, **type**, **order**, and **field** (for *onChange* scripts)
12+
- Exist within the same table
13+
- Are still **active** in the system
14+
- By running this audit, you can quickly detect redundant client scripts that may have been unintentionally cloned
15+
16+
17+
### Functionality
18+
Uses a Background Script to:
19+
- Query all active client scripts for the selected table
20+
- Sort them by creation date (oldest to newest)
21+
- Detect duplicates with identical configurations
22+
- Display results as:
23+
- [ORIGINAL] – first created client script
24+
- This script has X duplicates – count of newer scripts with the same trigger setup
25+
- If no duplicates are found, a message confirms:
26+
- No duplicate client scripts found for this table
27+
28+
### Steps to Run
29+
30+
1. Navigate to **System Definition → Scripts - Background**
31+
2. Click **New**
32+
3. Paste the provided script and modify this line as needed:
33+
- var targetTable = 'incident'; // or 'change_request', 'wm_order' etc....
34+
4. Run Script
35+
36+
---
37+
38+
### Example:1 Run The Bacground Script For Change Request Table
39+
40+
![Duplicate Client Script Audit Output](Backgroundscript_clientscript_duplicate_4.png)
41+
42+
---
43+
44+
### Cross Checking The result from output of Script Run
45+
46+
![Duplicate Client Script Audit Output](Backgroundscript_clientscript_duplicate_1.png)
47+
48+
---
49+
50+
### Example:2 Run The Bacground Script For Incident Table
51+
52+
![Duplicate Client Script Audit Output](Backgroundscript_clientscript_duplicate_3.png)
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Target table
2+
var targetTable = 'change_request'; // < can be incident, change_request , wm_order etc.... >
3+
4+
// counters
5+
var seen = {};
6+
var duplicateCount = 0;
7+
var totalCount = 0;
8+
9+
// Query ACTIVE Client Scripts sorted by creation date (oldest → newest)
10+
var gr = new GlideRecord('sys_script_client');
11+
gr.addQuery('table', targetTable);
12+
gr.addQuery('active', true);
13+
gr.orderBy('sys_created_on');
14+
gr.query();
15+
16+
gs.print('--- Duplicate Client Script Audit for table: ' + targetTable + ' ---');
17+
gs.print('Sorted by creation date (oldest → newest)');
18+
gs.print('MODE: Detection only (no updates performed).');
19+
gs.print('');
20+
21+
// group Client Scripts
22+
while (gr.next()) {
23+
totalCount++;
24+
25+
// Build unique trigger key
26+
var key = gr.name + '_' + gr.ui_type + '_' + gr.type + '_' + gr.order;
27+
if (gr.type == 'onChange') key += '_' + gr.field;
28+
29+
// Build readable script info
30+
var info = gr.name +
31+
' | Type: ' + gr.type +
32+
(gr.type == 'onChange' ? ' | Field: ' + gr.field : '') +
33+
' | Order: ' + gr.order +
34+
' | Created: ' + gr.sys_created_on.getDisplayValue() +
35+
' | Sys_id: (' + gr.sys_id + ')';
36+
37+
38+
// If first occurrence consider it as key
39+
if (!seen[key]) {
40+
seen[key] = {
41+
original: info,
42+
duplicates: []
43+
};
44+
}
45+
// If key already exists put into duplicate
46+
else {
47+
seen[key].duplicates.push(info);
48+
duplicateCount++;
49+
}
50+
}
51+
52+
// grouping the scripts
53+
var groupsWithDuplicates = 0;
54+
for (var key in seen) {
55+
var group = seen[key];
56+
if (group.duplicates.length > 0) {
57+
groupsWithDuplicates++;
58+
59+
// Original
60+
gs.print(group.original);
61+
62+
// Summary line
63+
// gs.print('This ' + seen.key + 'script has ' + group.duplicates.length + ' duplicate' );
64+
var trimmedKey = key.split('_')[0];
65+
gs.print('This ' + trimmedKey + ' script has ' + group.duplicates.length + ' duplicate' + (group.duplicates.length > 1 ? 's.' : '.'));
66+
67+
// Separator
68+
gs.print('--------------------------------------');
69+
}
70+
}
71+
72+
// no duplicates found
73+
if (groupsWithDuplicates === 0) {
74+
gs.print('No duplicate client scripts found for this table.');
75+
}
76+
77+
// ✅ Final summary
78+
gs.print('\n--------------------------------------');
79+
gs.print('✅ SUMMARY for table: ' + targetTable);
80+
gs.print('Total active scripts scanned: ' + totalCount);
81+
gs.print('Originals with duplicates: ' + groupsWithDuplicates);
82+
gs.print('Total duplicates detected: ' + duplicateCount);
83+
gs.print('--------------------------------------');

0 commit comments

Comments
 (0)