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
Tera Sage
Tera Sage

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

Reach out us on +91 6304422358 for complete training!! Explore SERVICENOW UDEMY COURSES -- YOUR RATING IS TRULY APPRECIATED!! SERVICENOW SCRIPTING COURSE: https://www.udemy.com/course/servicenow-scripting-mastery-advanced-development/ SERVICENOW ADMIN COURSE: ...