Detecting Duplicate Client Scripts Using a Background Script

Mohammed8
Tera Expert

 

Hi All, This is my first try at writing article here on the community, its about a Client Script Duplicate Audit script.

 

This background script identifies duplicate client scripts on specific tables such as incident, change_request, etc. Its goal is to find duplicate client scripts. Initially I thought to logic run on the sys_script_client table itself but the output was too cluttered, so to make it clearer, a target table variable was used to input TableName.

 

The script checks only active Client Scripts and compares key attributes like name, UI type, type (onLoad, onChange, onSubmit), order, and field (for onChange). Scripts sharing the same combination are grouped as duplicates, helping to detect duplicate client script.

 

var targetTable = 'change_request'; // < can be incident, change_request, wm_order, etc. >

var seen = {};
var duplicateCount = 0;
var totalCount = 0;

var clientAudit = new GlideRecord('sys_script_client');
clientAudit.addQuery('table', targetTable);
clientAudit.addQuery('active', true);
clientAudit.orderBy('sys_created_on');
clientAudit.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('');

while (clientAudit.next()) {
totalCount++;

var key = clientAudit.name + '_' + clientAudit.ui_type + '_' + clientAudit.type + '_' + clientAudit.order;
if (clientAudit.type == 'onChange') key += '_' + clientAudit.field;

var info = clientAudit.name +
' | Type: ' + clientAudit.type +
(clientAudit.type == 'onChange' ? ' | Field: ' + clientAudit.field : '') +
' | Order: ' + clientAudit.order +
' | Created: ' + clientAudit.sys_created_on.getDisplayValue() +
' | Sys_id: (' + clientAudit.sys_id + ')';

if (!seen[key]) {
seen[key] = {
original: info,
duplicates: []
};
}
// If key already exists, put into duplicate
else {
seen[key].duplicates.push(info);
duplicateCount++;
}
}

var groupsWithDuplicates = 0;
for (var key in seen) {
var group = seen[key];
if (group.duplicates.length > 0) {
groupsWithDuplicates++;

gs.print(group.original);

var trimmedKey = key.split('_')[0];
gs.print('This ' + trimmedKey + ' script has ' + group.duplicates.length + ' duplicate' + (group.duplicates.length > 1 ? 's.' : '.'));
gs.print('--------------------------------------');
}
}

if (groupsWithDuplicates === 0) {
gs.print('No duplicate client scripts found for this table.');
}

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('--------------------------------------');

 

The script makes no changes to records. It simply lists each duplicate group with key details such as the script name, type, order, creation date, and sys_id, followed by a summary of the total scripts scanned, the originals with duplicates, and the overall duplicates detected

 

Mohammed8_0-1762929366476.png 

 

 

 

 

 

 

 

I’m open to ideas and feedback, so feel free to share any suggestions in the comments. If you found this post helpful or even a little musing, please mark it helpful, it keeps the learning going!

 

Thanks and Regards,

Mohammed Zakir

0 REPLIES 0