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 delete duplicate records in servicenow

vbbfhg
Kilo Contributor

How to delete duplicate records in servicenow

3 REPLIES 3

Amit Gujarathi
Giga Sage
Giga Sage

Hi @vbbfhg ,
I trust you are doing great.
Here's a basic example of how you might write a script to delete duplicate records. This example assumes you are dealing with duplicate incidents based on a similar field (like 'short_description'):

var gr = new GlideRecord('incident'); // Replace 'incident' with your table
gr.addAggregate('COUNT', 'short_description'); // Replace 'short_description' with the field you're checking
gr.groupBy('short_description');
gr.query();

while (gr.next()) {
    if (gr.getAggregate('COUNT', 'short_description') > 1) {
        var dupeGr = new GlideRecord('incident'); // Replace 'incident' with your table
        dupeGr.addQuery('short_description', gr.short_description); // Replace 'short_description' with the field you're checking
        dupeGr.query();
        var i = 0;
        while (dupeGr.next()) {
            if (i != 0) { // Skip the first record, delete the rest
                dupeGr.deleteRecord();
            }
            i++;
        }
    }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



1st line is incorrect I think it'll be Glideaggregate

 

  

Harish Bainsla
Kilo Patron
Kilo Patron

Hi @vbbfhg  please check below link and do modification as per your need

https://www.servicenow.com/community/servicenow-impact-forum/delete-the-duplicate-records-from-incid...

https://www.youtube.com/watch?v=Y4m_yquAMhk

if my answer helps you mark helpful and accept solution