Find Duplicate hardware model using GlideAggregate
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 02:15 AM
Hi I have written below fix script to find duplicate hardware model but I am using glideRecord to get the sysid. Can we use GlideAggregate only to get the sysid of duplicate record
var duplicateIds = [];
var aggregate = new GlideAggregate('cmdb_hardware_product_model');
aggregate.addEncodedQuery('manufacturerISNOTEMPTY^nameISNOTEMPTY');
aggregate.addAggregate('COUNT', 'sys_id');
aggregate.groupBy('name'); // Count the number of records with the same name and manufacturer
aggregate.groupBy('manufacturer');
aggregate.addHaving('COUNT', '>=', 2);
aggregate.query();
while (aggregate.next()) {
var duplicateQuery = new GlideRecord('cmdb_hardware_product_model');
duplicateQuery.addQuery('name', aggregate.getValue('name'));
duplicateQuery.addQuery('manufacturer', aggregate.getValue('manufacturer'));
duplicateQuery.query();
while (duplicateQuery.next()) {
duplicateIds.push(duplicateQuery.sys_id.toString());
}
}
gs.print('Duplicate sys_ids: ' + duplicateIds.join(', '));
2 REPLIES 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 02:48 AM
Hi,
Those are duplicates but different records meaning two or more separate records. For that you have to do the GlideRecord.
Thanks and Regards,
Saurabh Gupta
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 02:51 AM
Hi Ujalwa,
Please try below and check based on requirement
aggregate.addAggregate('COUNT_DISTINCT', 'sys_id');
Please mark useful if my reply helped you