Find Duplicate hardware model using GlideAggregate

UjwalaBedarkar
Tera Expert

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

Saurabh Gupta
Kilo Patron
Kilo Patron

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

Jagadish Sanadi
Kilo Sage

Hi Ujalwa,

 

Please try below and check based on requirement 

aggregate.addAggregate('COUNT_DISTINCT', 'sys_id');

 

Please mark useful if my reply helped you