Get Duplicate Records from any table
Sometimes it is useful to be able in a report to collect duplicate records.
For example to find duplicates af Assets with the same Asset Tag or CI:s with identical name
Here is an example where you can create a dynamic Script Include that then can be used in reports.
Script Include
Name: getDuplicatesClient callable: true
Description: Get Duplicates from any table in the instance to use in Reports. In Report syntax specify the table and field.
Script:
var getDuplicates = Class.create();getDuplicates.prototype = Object.extendsObject(AbstractAjaxProcessor, { getDuplicates: function(table, field) { var dupRecords = []; var gaDupCheck = new GlideAggregate(table); gaDupCheck.addAggregate('COUNT', field); gaDupCheck.addNotNullQuery(field); gaDupCheck.groupBy(field); gaDupCheck.addHaving('COUNT', '>', 1); gaDupCheck.query(); while (gaDupCheck.next()) { dupRecords.push(gaDupCheck[field].toString()); } return dupRecords;
},
type: 'getDuplicates'
});
In a report then Example.
Duplicate CI:s on name
Create a report on the cmdb_ci_computer table.
set the filter as: Name is javascript:new global.getDuplicates().getDuplicates('cmdb_ci_computer', 'name')
If you wanna do this on another table just change the the table to report on and change the syntax to that table and fieldname in the condition
Good luck
Anders
Labels:
https://www.servicenow.com/community/platform-analytics-articles/get-duplicate-records-from-any-table/ta-p/2304670
