Background Script - Duplicate Numbers

Shirley3476
Kilo Contributor

I am writing a script (most of which was written for me) to renumber the tickets in our system that have duplicate numbers.
I am able to get them to print onscreen, but to get them to actually renumber I need to add currentGlideRecord.update(); in the processing. I am not sure if its working as I still see dups.
Would love for anone to look at my script and provide feedback.

// start background script

fixDuplicateRecords();

function fixDuplicateRecords(){

var uniqueDuplicates = "";

var duplicateNumbers = 'CHG0013084,GAPRV0010196,INC0056038,INC0056040,INC0056041,INC0056043,INC0056045,INC0056046,INC0056047,INC0056048,INC0056050,INC0056051,INC0056053,INC0056054,INC0056055,INC0056056,INC0056058,INC0056059,INC0056060,INC0056061,INC0056062,INC0056063,INC0056064,INC0056065,INC0056066,INC0056068,INC0056069,INC0056071,INC0056072,INC0056074,INC0056075,INC0056076,INC0056081,INC0056082,INC0056083,INC0056084,INC0056085,INC0056086,INC0056087,INC0056090,INC0056091,INC0056092,INC0056095,INC0056096,INC0056097,INC0056098,INC0056099,INC0056100,INC0056101,INC0056102,INC0056105,INC0056106,INC0056107,INC0056108,INC0056109,INC0056110,INC0056111,INC0056113,INC0056115,INC0056116,INC0056117,INC0056118,INC0056120,INC0056121,INC0056122,INC0056125,INC0056126,INC0056127,INC0056128,INC0056129,INC0056130,INC0056131,INC0056132,INC0056135,TASK0010154,TASK0010155,TASK0010156,TASK0010157,TASK0010158,TASK0010159,TASK0010160,TASK0010161,TASK0010162,TASK0010163,TASK0010164,TASK0010165,TASK0010166,TASK0010167,TASK0010169,TASK0010170,TASK0010171,TASK0010172,TASK0010173,TASK0010174,TASK0010175,TASK0010176,TASK0010177,TASK0010178,TASK0010179,TASK0010180,TASK0010181,TASK0010183,TASK0010184,TASK0010185,TASK0010186,TASK0010187,TASK0010188,TASK0010189,TASK0010190,TASK0010191,TASK0039148,TASK0039149,TASK0039150,TASK0039151,TASK0039152,TASK0039153,TASK0039154,TASK0039155,TASK0039156,TASK0039157,TASK0039158,TASK0039159,TASK0039160,TASK0039161,TASK0039162,TASK0039163,TASK0039164,TASK0039165,TASK0039166,TASK0039167,TASK0039168,TASK0039169,TASK0039170,TASK0039171,TASK0039172,TASK0039173,TASK0039174,TASK0039175,TASK0039176,TASK0039177,TASK0039178,TASK0039179,TASK0039180,TASK0039181,TASK0039182,TASK0039183,TASK0039184,TASK0039185,TASK0039186,TASK0039187,TASK0039188,TASK0039189,TASK0039190,TASK0039191,TASK0039192,TASK0039193,TASK0039194,TASK0039195,TASK0039196,TASK0039197,,';

/*
* CHANGES
*/
var changes = new GlideRecord('change_request');
changes.addQuery('number','IN', duplicateNumbers);
changes.query();
while(changes.next()){
processRecords(changes,"change_request");
}

/*
* GROUP APPROVAL
*/
var approvalGroups = new GlideRecord('sysapproval_group');
approvalGroups.addQuery('number','IN', duplicateNumbers);
approvalGroups.query();
while(approvalGroups.next()){
processRecords(approvalGroups,"sysapproval_group");
}

/*
* INCIDENTS
*/
var incidents = new GlideRecord('incident');
incidents.addQuery('number','IN', duplicateNumbers);
incidents.query();
while(incidents.next()){
processRecords(incidents,"incident");
}

/*
* SERVICE-NOW TASKS
*/
var sncTasks = new GlideRecord('u_sn_task');
sncTasks.addQuery('number','IN', duplicateNumbers);
sncTasks.query();
while(changes.next()){
processRecords(sncTasks,"u_sn_task");

}

/*
* TASK
*/
var tasks = new GlideRecord('task');
tasks.addQuery('number','IN', duplicateNumbers);
tasks.query();
while(tasks.next()){
processRecords(tasks,"task");
}

function processRecords(currentGlideRecord,glideRecordType){
// create an instance of the number manager
var nm = new NumberManager(glideRecordType);
// if this is the first instance of a duplicate record
if(uniqueDuplicates.indexOf(currentGlideRecord.number) == -1)
{

var oldNumber = currentGlideRecord.number +"";

gs.print("old duplicate number... " +oldNumber);

// re-number the record with the next un-used number
currentGlideRecord.number = nm.getNextObjNumberPadded();

gs.print("new unique number... " +currentGlideRecord.number);

// add the record to the list to prevent processing the same record again
if(uniqueDuplicates == ""){
uniqueDuplicates = oldNumber +"";
}else{
uniqueDuplicates = uniqueDuplicates + "," +oldNumber +"";
}
//gs.print("already processed... " +uniqueDuplicates);

currentGlideRecord.update();

}
}

}

// end background script


Please and Thank you,
Shirley

2 REPLIES 2

adiddigi
Tera Guru

Lifted from here. This is a beautifully written code 🙂
Duplicate record

Explanation :
I would use a Background Script to find and list all of these duplicates, then once you are sure you want to change the records that are returned, modify the script a bit to change the values you wish to change. If you are using LDAP synchronization you need to make sure you think about the coalesce field also. Here is a script that will find all duplicate user_name 's and print them on screen for you. You can modify it a bit to pickup what you need, then after you're sure it's only picking up records you want, modify it to change each of the records and update().



gatherDupes();

function gatherDupes() {
var ga = new GlideAggregate('sys_user');
ga.addAggregate('COUNT', 'user_name');
ga.addQuery('user_name', '!=', '');
ga.groupBy('user_name');
ga.addHaving('COUNT', '>', 1);
ga.query();
var dupNames = [];
gs.print('The sys_user table has ' + ga.getRowCount() + ' duplicates based on user_name field...');
while (ga.next()) {
gs.print(ga.user_name);
fixDuplicates(ga.user_name);
}
}
function fixDuplicates(userName) {
var gr = new GlideRecord('sys_user');
gr.addQuery('user_name', userName);
gr.orderByDesc('sys_created_on');
gr.query();
while (gr.next()) {
gs.print('-sys_id- ' + gr.sys_id + ' -email- ' + gr.email + ' -user_name- ' + gr.user_name);
}
}



Shirley3476
Kilo Contributor

Thank you - That helps!