diff --git a/Server-Side Components/Business Rules/Duplicate CI Detection for ServiceNow/DuplicateCIDetectorScriptInclude.js b/Server-Side Components/Business Rules/Duplicate CI Detection for ServiceNow/DuplicateCIDetectorScriptInclude.js new file mode 100644 index 0000000000..115fb3bd2d --- /dev/null +++ b/Server-Side Components/Business Rules/Duplicate CI Detection for ServiceNow/DuplicateCIDetectorScriptInclude.js @@ -0,0 +1,40 @@ +var DuplicateCIDetector = Class.create(); +DuplicateCIDetector.prototype = { + initialize: function() {}, + + findDuplicates: function(ciName, serialNumber, assetTag, sysId) { + var duplicates = []; + var gr = new GlideRecord('cmdb_ci'); + gr.addQuery('active', true); + + // Basic name match (case-insensitive) + if (ciName) { + gr.addQuery('name', 'LIKE', ciName); + } + + // Optional matching by serial or asset tag + if (serialNumber) { + gr.addOrCondition('serial_number', serialNumber); + } + if (assetTag) { + gr.addOrCondition('asset_tag', assetTag); + } + + if (sysId) + gr.addQuery('sys_id', '!=', sysId); // ignore current record + + gr.query(); + while (gr.next()) { + duplicates.push({ + name: gr.getValue('name'), + serial_number: gr.getValue('serial_number'), + asset_tag: gr.getValue('asset_tag'), + sys_id: gr.getUniqueValue() + }); + } + + return duplicates; + }, + + type: 'DuplicateCIDetector' +}; diff --git a/Server-Side Components/Business Rules/Duplicate CI Detection for ServiceNow/Readme.md b/Server-Side Components/Business Rules/Duplicate CI Detection for ServiceNow/Readme.md new file mode 100644 index 0000000000..9c69a78457 --- /dev/null +++ b/Server-Side Components/Business Rules/Duplicate CI Detection for ServiceNow/Readme.md @@ -0,0 +1,18 @@ +Duplicate CIs (Configuration Items) in the CMDB cause data redundancy, reporting errors, and incorrect impact analysis. +This project automatically detects potential duplicate CIs during record creation or update, using partial string matching and fuzzy comparison on fields like Name, Serial Number, or Asset Tag. + +It can warn the user, flag duplicates, or even prevent save depending on configuration. + +🚀 Features + +Detects duplicate Configuration Items on insert or update + +Matches based on configurable fields (e.g., name, serial_number, asset_tag) + +Uses partial/fuzzy matching (e.g., "Laptop123" ≈ "Laptop-123") + +Optionally prevents saving duplicate entries + +Works on both platform UI and Service Portal + +Extendable for custom CI classes (cmdb_ci_computer, cmdb_ci_server, etc.) diff --git a/Server-Side Components/Business Rules/Duplicate CI Detection for ServiceNow/beforeBusinessRule.js b/Server-Side Components/Business Rules/Duplicate CI Detection for ServiceNow/beforeBusinessRule.js new file mode 100644 index 0000000000..fc51cc5973 --- /dev/null +++ b/Server-Side Components/Business Rules/Duplicate CI Detection for ServiceNow/beforeBusinessRule.js @@ -0,0 +1,19 @@ +(function executeRule(current, previous /*null when async*/) { + var detector = new DuplicateCIDetector(); + var duplicates = detector.findDuplicates( + current.name, + current.serial_number, + current.asset_tag, + current.sys_id + ); + + if (duplicates.length > 0) { + var message = 'Potential duplicate CIs detected:\n'; + for (var i = 0; i < duplicates.length; i++) { + message += '- ' + duplicates[i].name + ' (Serial: ' + duplicates[i].serial_number + ')\n'; + } + + gs.addErrorMessage(message); + current.setAbortAction(true); // stop record save + } +})(current, previous);