- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2023 08:43 AM - edited 02-19-2023 10:51 AM
I have wriiten a background script to delete duplicates in a table on my PDI
Now kindly help with the logic to delete duplicate records.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2023 09:01 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2023 09:01 AM
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.