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.

How to check there are duplicate records on a table?

sonita
Giga Guru

I want to check if there are duplicate entries for 'asset tag' on asset table? there are more than 3000 records? is there a way to check that?

1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

You can create a bar or pie report and group by asset tag.  You could also run this script from the 'Scripts -> Background' module.

var table = 'alm_asset';
var field = 'asset_tag';
var gaDupCheck = new GlideAggregate(table); 
gaDupCheck.addAggregate('COUNT', field); 
gaDupCheck.addNotNullQuery(field); 
gaDupCheck.groupBy(field); 
gaDupCheck.addHaving('COUNT', '>', 1); 
gaDupCheck.query(); 
while (gaDupCheck.next()) { 
    gs.print(gaDupCheck.getValue(field) + ': ' + gaDupCheck.getAggregate('COUNT', field));
}

View solution in original post

2 REPLIES 2

ARG645
Tera Guru

Run this in your background Script. It will give you all the Tag names which have duplicate entries. It doesn't check for Duplicate records. 

gs.print(getDuplicates('alm_asset','asset_tag'));

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 Stanger
Giga Sage

You can create a bar or pie report and group by asset tag.  You could also run this script from the 'Scripts -> Background' module.

var table = 'alm_asset';
var field = 'asset_tag';
var gaDupCheck = new GlideAggregate(table); 
gaDupCheck.addAggregate('COUNT', field); 
gaDupCheck.addNotNullQuery(field); 
gaDupCheck.groupBy(field); 
gaDupCheck.addHaving('COUNT', '>', 1); 
gaDupCheck.query(); 
while (gaDupCheck.next()) { 
    gs.print(gaDupCheck.getValue(field) + ': ' + gaDupCheck.getAggregate('COUNT', field));
}