delete duplicate records in table using fix script

Nilofer1
Giga Expert

I have wriiten a background script to delete duplicates in a table on my PDI

Nilofer1_0-1676824916979.png

 

sc.png

Now kindly help with the logic to delete duplicate records.

1 ACCEPTED SOLUTION

Rajesh Chopade1
Mega Sage

Hi @Nilofer1 

 

Following should help you:

How to find duplicate records? 

 

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

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 and helpful if my answer helps you to resolve your issue.

 

Thank you

Rajesh.

View solution in original post

1 REPLY 1

Rajesh Chopade1
Mega Sage

Hi @Nilofer1 

 

Following should help you:

How to find duplicate records? 

 

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

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 and helpful if my answer helps you to resolve your issue.

 

Thank you

Rajesh.