re-number old records

vesta_satari
Kilo Explorer

Hi all,

I have a table that I want to update all existing records with an ID number. How can I do that with a script?

any ideas?
Thanks!

4 REPLIES 4

Kostya
Tera Guru

Hello vesta.satari,

you have to query all records in this table and set the number you want. Please refer to the https://wiki.servicenow.com/index.php?title=GlideRecord for quering the database.


// initialize new object of type incident
var gr = new GlideRecord('incident');
gr.query();

var num = 0;
// iterate through the result set
while(gr.next()) {
num++;
gr.setValue('number', 'NEWNUM' + num);

//save
gr.update();
}


Hit the Thumb Icon and/or mark as Correct, if my answer was correct. So you help others to see correct responses and I get fame 🙂

Cheers,
Kostya

Thanks! It is a handy suggestion!

But I did it this way:
var grp = new GlideRecord('sys_user_group');
grp.query();
while(grp.next()) {

if (grp.u_number == '') {
var nm = new NumberManager('sys_user_group');
grp.u_number = nm.getNextObjNumberPadded();
grp.update();
}
gs.print(grp.u_number);
}


Aaron40
Kilo Guru

I was tasked to do this once.. way back when I only had a couple months of experience with ServiceNow. That must have been one of the scariest things I've had to do for data manipulation that couldn't be done through update sets. This was the first time I had to change live data in a production environment.

The solution above should work just fine. It's better once you realize incident numbers aren't important in ServiceNow and you can change them to whatever you wand and things will be just fine in the end.


geoffcox
Giga Guru

You may wish to have them numbered in the order in which they were created.
before your query, add:



grp.orderBy('sys_created_on');