- 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-24-2019 10:03 PM
Hi,
It means that they are not entering your loop. Can you go to u_m2m_authorize_servers.list and there add a filter and check with these sys_ids. If there are any rows matching those sys_ids.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2019 08:22 AM
Hi asifnoor, yes I specify a server and group then I will get results, but I am not trying to specify a specific server/group. I am attempting to find ALL duplicates on that table. Thank you for chiming in.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2019 07:13 AM
So, I am not looking for a specific sys_id. I am attempting to delete ANY duplicate records.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2019 09:06 AM
Hi,
You can try using the following method:
First create a script include and find the duplicates
var gr=new GlideAggregate('u_m2m_authorize_servers');
var duplicates= [];
gr.addAggregate('COUNT','u_group');
gr.addAggregate('COUNT','u_server');
gr.groupBy('u_group');
gr.groupBy('u_server');
gr.addHaving('COUNT', '>=', 2);
gr.query();
while(gr.next())
{
var array_Duplicates= {};
array_Duplicates.group= ''+gr.group;
array_Duplicates.server= ''+gr.server;
duplicates.push(array_Duplicates);
gs.info(JSON.stringify(duplicates));
}
return JSON.stringify(duplicates);
In your script you change it accordingly:
var optionsObj=ScriptIncludenmae.function();// need to edit the name and function of the script Include
for (var i = 0; i < optionsObj.length; i++) {
var gr=new GlideRecord('u_m2m_authorize_servers');
gr.addQuery('u_group',optionsObj.group);
gr.addQuery('u_server',optionsObj.server);
gr.query();
var deleteCount = 0;
while(gr.next()) {
gr.deleteRecord();
deleteCount++;
gs.print('Record: ' + gr.u_group + gr.u_server + ' Deleted');
}
gs.print('Records Deleted = ' + deleteCount);
}
Hope this solution helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2019 10:58 AM
Hello aashishdasari, I like the idea of a script include. I received this error attempting to creat the script include "Could not save record because of a compile error: JavaScript parse error at line (18) column (7) problem = invalid return (<refname>; line 18)". This refers to the "return JSON.stringify(duplicates);" line 18. I am attempting to troubleshoot.