How to Fix duplicate numbers issue using background/Fix script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2023 12:27 PM
Hello Guys,
By running the below script in background scripts, we have identified we have 100+ contract records with duplicate record numbers.
And, For the records that share the same number, I need the more recent record to be renumbered to the next available number by using fix script/background script.
Any help with the code is much appreciated.
Script:
var dupRecords = [];
var gaDupCheck1 = new GlideAggregate('ast_contract');
gaDupCheck1.addAggregate('COUNT','number');
gaDupCheck1.groupBy('number');
gaDupCheck1.addHaving('COUNT', '>', 1);
gaDupCheck1.query();
while (gaDupCheck1.next()) {
dupRecords.push(gaDupCheck1.number.toString() + '\n');
}
gs.print(dupRecords);
Thanks,
Ben.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2023 01:30 AM
Hello @Ben42
Please check the below community articles.
It should help you in renumbering the duplicate records:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2023 04:06 AM
You can also check here for the script to renumber the duplicate records.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2023 04:12 AM
Hi @Ben42 ,
In the gs.print(dupRecords) you would get the array of the records which got created as duplicates. Add sys_ids and do as below. Do little bit modifications if you wish as per your need.
var dupRecords = [];
var dupSysId = [];
var gaDupCheck1 = new GlideAggregate('ast_contract');
gaDupCheck1.addAggregate('COUNT','number');
gaDupCheck1.groupBy('number');
gaDupCheck1.addHaving('COUNT', '>', 1);
gaDupCheck1.query();
while (gaDupCheck1.next()) {
dupRecords.push(gaDupCheck1.number.toString() + '\n');
dupSysId.push(gaDupCheck1.sys_id.toString() + '\n');
}
gs.print(dupRecords);
gs.print(dupSysId);
var latestNum = ' ';
var grLatestRecNum = new GlideRecord("ast_contract");
grLatestRecNum.orderByDesc();
grLatestRecNum.query();
if(grLatestRecNum.next()){
latestNum = grLatestRecNum.number;
}
for(i=0;i<dupSysId.length;i++){
var gr = new GlideRecord("ast_contract");
if(gr.get(dupSysId[i])){
gr.number = latestNum +1;
}
}
Please mark correct if my response has solved your query.
Cheers,
Mohammed Basheer Ahmed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2023 08:03 AM
Hello Basheer,
Thank you for the response. But, when I run the first part of script (in background script), I am not getting sys ids of records but getting only record numbers.
Am I missing something here?
Script:
var dupRecords = [];
var dupSysId = [];
var gaDupCheck1 = new GlideAggregate('ast_contract');
gaDupCheck1.addAggregate('COUNT','number');
gaDupCheck1.groupBy('number');
gaDupCheck1.addHaving('COUNT', '>', 1);
gaDupCheck1.query();
while (gaDupCheck1.next()) {
dupRecords.push(gaDupCheck1.number.toString() + '\n');
dupSysId.push(gaDupCheck1.sys_id.toString() + '\n');
}
gs.print(dupRecords);
gs.print(dupSysId);
Thanks,
Ben.