Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

GlideAggregate for detecting duplicate records

tonypod
Kilo Explorer

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.

 

  1. getDuplicates();  
  2. function getDuplicates() {  
  3.     var dupRecords = [];  
  4.     var gaDupCheck1 = new GlideAggregate('sys_user');  
  5.     gaDupCheck1.addQuery('active','true');  
  6.     gaDupCheck1.addAggregate('COUNT', 'user_name');  
  7.     gaDupCheck1.groupBy('user_name');  
  8.     gaDupCheck1.addHaving('COUNT', '>', 1);  
  9.     gaDupCheck1.query();  
  10.     while (gaDupCheck1.next()) {  
  11.           dupRecords.push(gaDupCheck1.user_name.toString());  
  12.     }  
  13.     gs.print(dupRecords);  
  14. }
15 REPLIES 15

Anurag Tripathi
Mega Patron
Mega Patron

Hi Tony,



I dont see a problem in the script, can you just tell me where are you running this code? (script include, job, BR etc...). As gs.pring will work on background script.



So try 2 things to cross check your data and query.



Run exactly this code in a background script


  1. var dupRecords = [];
  2.     var gaDupCheck1 = new GlideAggregate('sys_user');
  3.     gaDupCheck1.addQuery('active','true');
  4.     gaDupCheck1.addAggregate('COUNT', 'user_name');
  5.     gaDupCheck1.groupBy('user_name');
  6.     gaDupCheck1.addHaving('COUNT', '>', 1);
  7.     gaDupCheck1.query();
  8.     while (gaDupCheck1.next()) {
  9.           dupRecords.push(gaDupCheck1.user_name.toString());
  10.     }
  11.     gs.print(dupRecords);


to Verify the result,



go to the user table and group by user name(User Id) and find duplicates, if any.


-Anurag

my suggestion might seem bit lame but give it a try...



add ur script to a script include and call that script in the background script...


try printing the return value from the script


The script works for a single field (user_name).   I'd like to have it work across two fields: user_name, and email. I'm not sure how to modify the script to make that work.


I'd also be interested in this, had a need for it but couldn't figure it out.