Finding Duplicate Operational CI's in ServiceNow
Introduction: In a recent project, I encountered an issue where I needed to identify duplicate operational Configuration Items (CI's) in ServiceNow. This blog post will walk you through the steps I followed to address this problem, including the code snippet and the report I created to display the list of duplicate CI's.
Code Snippet:
To find duplicate CI's, I created a script include with the following code:
//Gaurav Kumar - to find duplicate values inside cmdb_ci function findDuplicateCi() { var duplicate = []; var gr = new GlideAggregate('cmdb_ci'); gr.addAggregate('COUNT', 'name'); gr.groupBy('name'); gr.addNotNullQuery('name'); gr.addHaving('COUNT', '>', 1); gr.query(); while (gr.next()) { duplicate.push(gr.getValue('name')); } var uniqueArray = []; for (var i = 0; i < duplicate.length; i++){ if(!(duplicate[i] in uniqueArray)){ uniqueArray.push(duplicate[i]); } } return uniqueArray; }
If you need to find the count of duplicate CI's, you can use the following code:
Explanation:
The code uses a GlideAggregate object to perform an aggregation on the cmdb_ci
table, specifically on the name
field. It groups the records by name
and filters out null values. By adding a "COUNT" aggregation and a condition to retrieve records with a count greater than 1, we can identify the duplicate CI's.
The script stores the duplicate CI names in an array, and then extracts the unique values to another array. The findDuplicateCi()
function returns the list of unique duplicate CI names, while the calDuplicateCi()
function returns the count of unique duplicate CI's.
Creating a Report: To further enhance the visibility of duplicate CI's, I created a report that groups them by class and presents them on a dashboard. This allows developers working on CI's to easily identify and address duplicate entries.
Conclusion: By using the provided code snippet and creating a report, I successfully identified duplicate operational CI's in ServiceNow. You can modify the code and conditions according to your specific needs. Feel free to leverage this solution in your ServiceNow instance to streamline your configuration item management and ensure data integrity.
0 Comments