CMDB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-06-2023 01:03 AM
Hello Experts,
I am using the below script to find duplicate CIs based on unique identifiers. For computer class, I am using serial numbers as a unique identifier. The below script working fine but it's giving me duplicate CIs from the child class as well.
for ex: if we want duplicate CIs of a computer it gives a computer CIs including a laptop CIs which is a child of the computer. How can I overcome this problem? Please guide me thank you.
gs.print(getDuplicates('cmdb_ci_computer','serial_number'));
function getDuplicates(tablename,val) {
var dupRecords = [];
var gaDupCheck = new GlideAggregate(tablename);
gaDupCheck.addAggregate('COUNT',val);
gaDupCheck.addNotNullQuery(val);
gaDupCheck.groupBy(val);
gaDupCheck.addHaving('COUNT', '>', 1);
gaDupCheck.query();
while (gaDupCheck.next()) {
dupRecords.push(gaDupCheck[val].toString());
}
return dupRecords
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-06-2023 01:11 AM
Hi @Mark Wood ,
Add addQuery for only class computer
function getDuplicates(tablename, val) {
var dupRecords = [];
var gaDupCheck = new GlideAggregate(tablename);
gaDupCheck.addAggregate('COUNT', val);
gaDupCheck.addNotNullQuery(val);
gaDupCheck.groupBy(val);
gaDupCheck.addQuery('sys_class_name', 'cmdb_ci_computer');
gaDupCheck.addHaving('COUNT', '>', 1);
gaDupCheck.query();
while (gaDupCheck.next()) {
dupRecords.push(gaDupCheck[val].toString());
}
return dupRecords;
}
var duplicates = getDuplicates('cmdb_ci', 'serial_number');
gs.print(duplicates);
Please mark it as solution proposed and helpful if its serves your purpose.
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-06-2023 02:05 AM - edited ‎11-06-2023 02:11 AM
This is great, but I don't think that Anand's query is required as you are already using the cmdb_ci_computer table. I would suggest adding an additional query for the Form factor, as this is where you differentiate laptops from desktops etc.
gaDupCheck.addQuery('form_factor', 'Desktop'); // Review your choice options to make this optimal

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-06-2023 04:18 AM
@Mark Wood Please update your script as follows. I have used the class path to show duplicates for only the computer class.
gs.print(getDuplicates('cmdb_ci_computer','serial_number','sys_class_path=/!!/!2/!('));
function getDuplicates(tablename,val,enq) {
var dupRecords = [];
var gaDupCheck = new GlideAggregate(tablename);
gaDupCheck.addAggregate('COUNT',val);
gaDupCheck.addEncodedQuery(enq);
gaDupCheck.addNotNullQuery(val);
gaDupCheck.groupBy(val);
gaDupCheck.addHaving('COUNT', '>', 1);
gaDupCheck.query();
while (gaDupCheck.next()) {
dupRecords.push(gaDupCheck[val].toString());
}
return dupRecords;
}
Hope this helps.