Delete the duplicate records from incident table

vinod96
Tera Contributor

Delete the duplicate records from incident table based on short description could you please help any one

4 REPLIES 4

Danish Bhairag2
Tera Sage
Tera Sage

Hi @vinod96 ,

 

Please try the below script in order to delete the duplicate records.

 

var gr1;
var gr = new GlideAggregate('incident');
gr.groupBy('short_description');  
gr.query();
while (gr.next()) {
  gr1 = new GlideRecord('incident');  
  gr1.addQuery('short_description', gr.short_description);  
  gr1.query();  
  gr1.next(); // Skip the first result  
  while (gr1.next()) { // delete the next one
       gr1.deleteRecord();  
    }  
}  

 

Please mark my answer helpful & accepted if it helps you resolve your query.

 

Thanks,

Danish 

this might help:

var gaDelete = new GlideAggregate('TableName'); // table on which deletion is to be performed

gaDelete.groupBy('XXXXXXX'); // column which you think has duplicate values

gaDelete.setLimit(10000)
gaDelete.query();

var grData;

while (gaDelete.next()) {


grData = new GlideRecord('table');
grData.addQuery('field', gaDelete.Field);


grData.query();


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


while (grData.next()) { // delete the next one

// gs.print(''whatver you want to output)

grData.deleteRecord();

}

}

 

Riya Verma
Kilo Sage
Kilo Sage

Hi @vinod96 ,

 

Hope you are doing great.

 

To remove duplicate records from the incident table based on the short description, we can use a script to identify and delete the duplicates

var gr = new GlideAggregate('incident');
gr.addAggregate('COUNT', 'short_description');
gr.groupBy('short_description');
gr.addHaving('COUNT', '>', 1);
gr.query();

while (gr.next()) {
    var dupGr = new GlideRecord('incident');
    dupGr.addQuery('short_description', gr.getValue('short_description'));
    dupGr.orderByDesc('sys_updated_on');
    dupGr.query();
    dupGr.next(); // Move to the second record
    dupGr.deleteMultiple();
}

 
 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Nivedita Patil
Mega Sage
Mega Sage

Hi @vinod96 ,

 

Please refer the  below link.

 

https://www.servicenow.com/community/developer-forum/i-want-to-delete-duplicate-records-from-table/m...

 

Mark my answer correct and helpful if helps you.

 

Thanks,

Nivedita Patil.