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
01-18-2018 11:50 AM
Please mark the answer helpful if it really helped you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2016 03:00 AM
You can use multiple groupBy statements in GlideAggregate (see GlideAggregate - ServiceNow Wiki)
E.g. to find duplicate relations:
var gaDupCheck = new GlideAggregate('cmdb_rel_ci');
gaDupCheck.groupBy('parent');
gaDupCheck.groupBy('child');
gaDupCheck.groupBy('type');
gaDupCheck.addHaving('COUNT', '>', 1);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2017 11:38 AM
The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here: GlideAggregate
Visit http://docs.servicenow.com for the latest product documentation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2018 07:21 PM
This background script helped me find duplicate users with same employee number, based on example above.
var gaDupCheck = new GlideAggregate('sys_user');
gaDupCheck.addAggregate('COUNT', 'employee_number');
gaDupCheck.addNotNullQuery('employee_number');
gaDupCheck.groupBy('employee_number');
gaDupCheck.addHaving('COUNT', '>', 1);
gaDupCheck.query();
while (gaDupCheck.next()) {
gs.log(gaDupCheck.employee_number);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2020 01:14 AM
Hello,
I have the same requirement .i have written the fix script .it is working also .
var usr = new GlideAggregate('sys_user');
usr.addAggregate('COUNT', 'name');
usr.groupBy('name');
usr.addHaving('COUNT', '>', 1);
usr.query();
while (usr.next())
{
var usr1 = usr.name;
var gr = new GlideRecord('sys_user');
gr.addQuery('name',usr1);
gr.query();
gr.next();
while(gr._next())
{
gr.name = gr.name +' '+'('+gr.user_name+')';
gr.setWorkflow(false);
gr.update();
}
}
Regards,
Namrata Agrawal
kindly correct my answer if it is working for you as well so will helpful for other memebers .