GlideAggregate for detecting duplicate records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2014 08:41 AM
I am trying to detect duplicates across multiple columns using GlideAggregate, but cannot seem to figure it out. Is it even possible? Is there a better way? Here is a snippet for getting unique records when determining uniqueness from single column. I need to do this by looking at two columns.
- getDuplicates();
- function getDuplicates() {
- var dupRecords = [];
- var gaDupCheck1 = new GlideAggregate('sys_user');
- gaDupCheck1.addQuery('active','true');
- gaDupCheck1.addAggregate('COUNT', 'user_name');
- gaDupCheck1.groupBy('user_name');
- gaDupCheck1.addHaving('COUNT', '>', 1);
- gaDupCheck1.query();
- while (gaDupCheck1.next()) {
- dupRecords.push(gaDupCheck1.user_name.toString());
- }
- gs.print(dupRecords);
- }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2021 08:25 AM
Thank you, this was just what I needed to get me to the next step as a new ServiceNow developer!
I'm working on a script to find duplicate CI's and create remediation tasks for each group of them. Here's what I have so far:
var ga = new GlideAggregate('cmdb_ci_hardware');
ga.addAggregate('COUNT', 'serial_number');
ga.addHaving('COUNT', 'serial_number', '>', '1');
ga.query();
while (ga.next()) {
var deviceSN = ga.getValue('serial_number');
var gr = new GlideRecord('cmdb_ci_hardware');
gr.addQuery('serial_number',deviceSN);
gr.query();
while(gr.next()) {
deviceName = gr.getValue('name');
gs.print("Name: " + deviceName + ", Serial Number: " + deviceSN);
}
}
The next step is to incorporate the following sample code...
// where sys-id1 and sys-id2 are sys_IDs of CIs in the cmdb_ci table var sysIDs = 'sys-id1, sys-id2'; var dupTaskUtil = new CMDBDuplicateTaskUtils(); var deDupTaskID = dupTaskUtil.createDuplicateTask(sysIDs); gs.info(deDupTaskID);
...I'll post further about what I come up with...
Thanks!
Jason