Need Help Migrating Data Into New Records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2025 07:37 AM - edited 03-14-2025 07:56 AM
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()) {
}
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";
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2025 08:22 AM
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.
- Creates a mapping of old variable sys_id → new variable sys_id.
- Fetches all old question_answer records where question is in the list of old variable sys_ids.
- 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
- Inserts the new record into the question_answer table
- 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]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2025 07:56 AM
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?