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.

Need Help identifying Duplicate records on custom table

Sunny14
Tera Contributor

Hello Team,

 

We have a custom Table X and for some reason the numbering has started creating duplication at the insert of the record. I need help identifying duplicate records. The number of records are huge 900K+ so Group by will take forever. 

 

If we can printout the duplicate records via script, that would be good. After that we can work on those duplicates. 

 

Thanks. 

1 ACCEPTED SOLUTION

Ravi Peddineni
Kilo Sage

@Sunny14 

Try this in the Background Scripts:

 

gs.info(getDuplicateValues(<table>, <field>));

function getDuplicateValues(table, field){
	var dupValues = [];
	var ga = new GlideAggregate(table);
	ga.addAggregate('COUNT', field);
	ga.addNotNullQuery(field);
	ga.groupBy(field);
	ga.addHaving('COUNT', '>', 1);
	ga.query();
	while (ga.next()) {
		dupValues.push(ga[field].toString());
	}
	return dupValues;
}

 

 

****NOTE: It might take a lot of time to complete the query and might even get you session resources locked for this operation. So I strictly suggest you to run this in the possible lower instance where the criticality is low. ****

 

Reference: Get Duplicate Records from any table 

View solution in original post

6 REPLIES 6

Add this line before ga.query() function call:

ga.addEncodedQuery(“active=true”)

Thanks @Ravi Peddineni  I will try out.