Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need Help Migrating Data Into New Records

neil_b
Tera Guru

Hi everyone!

 

I am trying to do some maintenance on our record producers and identified 3 record producers that share the same 4 variables, so to clean this up I created a variable set and included those 4 variables in a variable set and deactivated the 12 individual variables (4 variables * 3 record producers).

 

My predicament now, is that I'm not sure how I would go about migrating the data that has been entered in the question_answer table, to the new variable set, as these record producers have been in production for some time.

 

I have the sys_ids of the old variable and the new variable, but I don't know how to generate a new question_answer record for each request that was submitted and copy the old value from the old question_answer record to the new record. I got a script started but I got stuck.

 

 

var gr = new GlideRecord('question_answer');
gr.addEncodedQuery('table_sys_id=[enter sys_id here]');
gr.query();
while(gr.next()) {
}

 

 

This is a record of the old variable. I need to know how to copy this value, generate a new question_answer record using the new variable set, and pasting the value in the record record.
QA Table.png
Here are the variables; the old ones were the individual variables, and the new ones are the ones that are now in a variable set.

Old Variable 1 Name & Sys_ID: "First Name" | "ABC";
New Variable 1 Name & Sys_ID: "First Name" | "123";

 

Old Variable 2 Name & Sys_ID: "Middle Name" | "DEF";
New Variable 2 Name & Sys_ID: "Middle Name" | "456";

 

Old Variable 3 Name & Sys_ID: "Last Name" | "GHI";
New Variable 3 Name & Sys_ID: "Last Name" | "789";

 

Old Variable 4 Name & Sys_ID: "Suffix" | "JKL";
New Variable 4 Name & Sys_ID: "Suffix" | "012";

2 REPLIES 2

KKM
Tera Guru

Hi Neil,

You need a script that migrates question_answer records from old variables to new variables while maintaining the request association. Below is a GlideRecord script to do this efficiently.

 Migration Script

This script will:  Find all records using the old variable sys_ids.
Create new question_answer records using the new variable sys_ids.
Copy over the values from the old variables to the new ones.

 

var oldToNewVarMap = { "ABC": "123", // First Name "DEF": "456", // Middle Name "GHI": "789", // Last Name "JKL": "012" // Suffix }; var grOldQA = new GlideRecord('question_answer'); grOldQA.addQuery('question', 'IN', Object.keys(oldToNewVarMap)); // Fetch only old variable records grOldQA.query(); while (grOldQA.next()) { var newVarSysId = oldToNewVarMap[grOldQA.question.toString()]; if (newVarSysId) { var grNewQA = new GlideRecord('question_answer'); grNewQA.initialize(); grNewQA.request_item = grOldQA.request_item; // Associate with the same request grNewQA.question = newVarSysId; // New variable sys_id grNewQA.value = grOldQA.value; // Copy the old value grNewQA.insert(); gs.info("Migrated value for Request: " + grOldQA.request_item + " | Old Variable: " + grOldQA.question + " | New Variable: " + newVarSysId); } }
How This Works
  1. Creates a mapping of old variable sys_id → new variable sys_id.
  2. Fetches all old question_answer records where question is in the list of old variable sys_ids.
  3. Loops through each old record and creates a new record in question_answer:
    • Assigns it to the same request_item
    • Uses the new variable's sys_id
    • Copies over the value
  4. Inserts the new record into the question_answer table
  5. Next Steps
  • Test on a Dev instance before running in Production.

  • Disable Business Rules on question_answer if they interfere.

  • Verify migrated data using a query like:

     
    var grCheck = new GlideRecord('question_answer'); grCheck.addQuery('question', 'IN', ["123", "456", "789", "012"]); // New Variables grCheck.query(); while (grCheck.next()) { gs.info("Request: " + grCheck.request_item + " | Variable: " + grCheck.question + " | Value: " + grCheck.value); }

This should successfully migrate the variable data to your new Variable Set.

This should solve your issue!

Kindly mark it as "Accepted Solution"/"helpful", as it resolves your query. Please press like button for the resolution provided.

With Regards,
Krishna Kumar M - Talk with AIT3ch
LinkedIn: https://www.linkedin.com/in/mkrishnak4/
YouTube: https://www.youtube.com/@KrishAIT3CH
Topmate: https://topmate.io/mkrishnak4 [ Connect for 1-1 Session]

Thank you @KKM for the elaborate script! I will test this out in DEV momentarily! Could I just run this as a background script or do I have to implement it using a different method?