Skip to content

Commit 5e7ac1f

Browse files
authored
Create backGroundScriptDuplicateClientScript.js
1 parent f4351b0 commit 5e7ac1f

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
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)