- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2019 09:47 AM
Hi,
So I've scrubbed through the forums and found many scripts to delete duplicate records, but they are all based on 1 field. Some of which are using GlideAggregate. So I need a script to delete records based on several field criteria. Example
Field 1 Field 2
John Smith
John Doe
John Smith
John Johnson
Delete the Duplicate John Smiths.
I will be using this script as a fix script running on a schedule as maintenance for an m2ms table. The fields are reference fields if that matters.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2019 03:26 PM
Blade,
Try using the below code:
var optionsObj=JSON.parse(FindDuplicateRecords());
for (var i = 0; i < optionsObj.length; i++) {
var gr=new GlideRecord('u_m2m_authorize_servers');
gr.addQuery('u_group',optionsObj[i].group);
gr.addQuery('u_server',optionsObj[i].server);
gr.query();
var deleteCount = gr.getRowCount();
while(gr.next()) {
if(deleteCount!=1)
{
gr.deleteRecord();
deleteCount--;
gs.info('Record: ' + gr.optionsObj[i].group + gr.optionsObj[i].server + ' Deleted');
gs.info('Records Deleted = ' + deleteCount);
}
}
}
If the solution is helpful please mark it as helpful

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2019 02:07 PM
I think you are missing to iterate though object
Try this
var optionsObj=JSON.parse(FindDuplicateRecords());
for (var i = 0; i < optionsObj.length; i++) {
var gr=new GlideRecord('u_m2m_authorize_servers');
gr.addQuery('u_group',optionsObj[i].group);
gr.addQuery('u_server',optionsObj[i].server);
gr.query();
var deleteCount = 0;
while(gr.next()) {
gr.deleteRecord();
deleteCount++;
gs.info('Record: ' + gr.optionsObj[i].group + gr.optionsObj[i].server + ' Deleted');
gs.info('Records Deleted = ' + deleteCount);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2019 02:40 PM
That is progress, but now it finds the duplicates and deletes the duplicate + the last remaining record of that duplicate. So there is no record left.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2019 02:47 PM
As you are using while loop it will remove all the matches
I think you are missing to iterate though object
Try this
var optionsObj=JSON.parse(FindDuplicateRecords());
for (var i = 0; i < optionsObj.length; i++) {
var gr=new GlideRecord('u_m2m_authorize_servers');
gr.addQuery('u_group',optionsObj[i].group);
gr.addQuery('u_server',optionsObj[i].server);
gr.query();
var deleteCount = 0;
if(gr.next()) {
gr.deleteRecord();
deleteCount++;
gs.info('Record: ' + gr.optionsObj[i].group + gr.optionsObj[i].server + ' Deleted');
gs.info('Records Deleted = ' + deleteCount);
}
}
But again the above script will one remove single record. Let's say if there are 3 reocrds with same group and server it will only remove 1 record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2019 03:18 PM
It removed 1 record as you stated. So now the issue is how do I delete all the duplicates in the table while leaving 1 record for said duplicate in the table.
Thank you so far. Progress.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2019 02:14 PM
What dvp has suggested is true. It is missing the iterate object:
Please change it from:
var gr=new GlideRecord('u_m2m_authorize_servers');
gr.addQuery('u_group',optionsObj.group);
gr.addQuery('u_server',optionsObj.server);
To:
var gr=new GlideRecord('u_m2m_authorize_servers');
gr.addQuery('u_group',optionsObj[i].group);
gr.addQuery('u_server',optionsObj[i].server);