How to retrieve duplicate records & How to delete duplicate records ?

Shikha puri
Tera Contributor
  1. How to retrieve duplicate records?
  2. How to delete duplicate records?
1 REPLY 1

Community Alums
Not applicable

Hi @Shikha puri ,

Following should help you:

How to find duplicate records?

Duplicate Record Scripts — ServiceNow Elite

Following script written worked for me:

var testGr = new GlideAggregate('u_test_data');   // table on which deletion is to be performed

testGr.groupBy('u_test_data_column2');   // column which you think has duplicate values i think it would be the value column for sys_choice table

testGr.query();

var testGR1;

while (testGr.next()) {  

    testGR1 = new GlideRecord('u_test_data');  

    testGR1.addQuery('u_test_data_column2', testGr.u_test_data_column2);  

    testGR1.query();  

    testGR1.next(); // Skip the first result  

    while (testGR1.next()) { // delete the next one

           testGR1.deleteRecord();  

    }  

}  

you can test this script by running it against incident table:

Steps to execute/test this script:

1) In incident table create 2 records with same short_description

2) Modify the code above and check whether only 1 record is remaining and 1 got deleted.

var testGr = new GlideAggregate('incident');  

testGr.groupBy('short_description');  

testGr.query();

var testGR1;

while (testGr.next()) {  

    testGR1 = new GlideRecord('incident');  

    testGR1.addQuery('short_description', testGr.short_description);  

    testGR1.query();  

    testGR1.next(); // Skip the first result  

    while (testGR1.next()) { // delete the next one

           testGR1.deleteRecord();  

    }  

}  

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep