Changing a Number Prefix for Existing Records

//Changing a Number Prefix for Existing Records

Changing a Number Prefix for Existing Records

I

had a customer ask me this morning if it was possible to change the number prefix for records in the Change Request table. Specifically, they wanted to change the prefix from the default ‘CHG’ to ‘RFC’. The answer to this question is ‘Yes’, but there are a couple of things you need to do and be careful about…especially if you’re changing the prefix in a table that has existing records using the prefix. In this post, I’ll explain how you can adjust the number prefix of a table using the ‘Number Maintenance‘ module. I’ll also show how you can use a script to adjust the number prefix on existing records.

Number Prefix Change

Tip! I’ve also written before about adding a completely new number field and number values to existing records. If you’ve got existing data that needs a number, check out this post.

While it is possible to force a number maintenance record into an update set, the process described here should be done manually in all instances if it involves already-numbered records to avoid numbering conflicts.

The first step is to navigate to the ‘Number Maintenance’ module and find the record for your table. In this case, I’m looking for the record for the ‘change_request’ table as shown here…
Number Maintenance

Open the record to change the number prefix. In this example I’m changing the number prefix from ‘CHG’ to ‘RFC’. Making this change will adjust the prefix for all records that will be created on the change request table in the future.

Number Prefix Change

Modifying the prefix in the number maintenance record has NO EFFECT on existing records however. To adjust the prefix on existing records you’ll need to use a script. You can run any arbitrary script in your system by navigating to ‘System Definition -> Scripts-Background’, entering your script, and clicking the ‘Run script’ button.
Be careful though! You really need to make sure you’ve tested your solution and you know what you’re doing any time you run a script like this. You should also plan to run this script during off hours if you’ve got thousands of records that you’ll be updating to avoid any negative performance impact that a mass-update may cause.

Here’s the script. It queries ALL change request records and updates all of them with the ‘RFC’ prefix. Note that I’m using the ‘setWorkflow’ and ‘autoSysFields’ flags so that other business rules and timestamps don’t get added.

//Query for change requests
var rec = new GlideRecord('change_request');
rec.query();
while(rec.next()){
   //Change the number prefix from 'CHG' to 'RFC'
   rec.number = rec.number.replace('CHG', 'RFC');
   rec.setWorkflow(false); //Do not run business rules
   rec.autoSysFields(false); //Do not update system fields
   rec.update();
}
By | 2018-07-09T14:59:55-06:00 December 28th, 2011|Categories: Scripting|Tags: |1 Comment

About the Author:

One Comment

  1. Jim Uomini January 23, 2015 at 5:15 pm - Reply

    One of our groups changed its name from OCM to OMC. This script was very handy.

    Thanks as always Mark.

    Best, Jim

Leave A Comment