How can we delete duplicate records?

Supriya P
Tera Contributor

How can we delete duplicate records from incident table using background script?

SupriyaP_0-1706851200909.png

 

4 REPLIES 4

Harish KM
Kilo Patron
Kilo Patron

Hi @Supriya P below script should do

var inc= new GlideAggregate('incident');

inc.groupBy('number'); // group by number
inc.query();
while (inc.next()) {

var inc1= new GlideRecord('incident');

inc1.addQuery('number', inc.number);

inc1.query();

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

while (inc1.next()) { // delete the next one
gs.info(inc1.number);
inc1.deleteRecord();

}

}

Regards
Harish

Nivedita Patil
Mega Sage
Mega Sage

Hi @Supriya P ,

 

please refer the below link.

https://www.servicenow.com/community/developer-forum/background-script-to-delete-duplicate-records/m...

 

Mark my answer as accepted solution and helpful if helps you.

 

Thanks,

Nivedita Patil.

Aniket Chavan
Tera Sage
Tera Sage

Hello @Supriya P ,

Give a try to the script below and see how it works for you.

var inc = new GlideAggregate('incident');

inc.groupBy('short_description', 'description'); // group by multiple fields
inc.query();

while (inc.next()) {
    var inc1 = new GlideRecord('incident');

    inc1.addQuery('short_description', inc.short_description);
    inc1.addQuery('description', inc.description);
    inc1.query();

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

    while (inc1.next()) { // delete the next one
        gs.print('Deleting duplicate incident with number: ' + inc1.number);
        inc1.deleteRecord();
    }
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

Thank you all!

Tried below script and issue solved

var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_created_onRELATIVELT@minute@ago@30');
gr.query();
while(gr.next()){
gr.deleteMultiple();
}