update variables

Rajiv7753Sutar
Tera Contributor

Variables for few catalogs have been updated to reference type without creating the new variables. resulting in missing data in the old RITM records. To resolve this we planned to create a new inactive variable of previous type for each of the affected variable for the affected catalog items and transfer the field values which are not displaying on the fields for which the type has changed as strings to these new deactivated variables. There are nearly 55 catalog items and 86 variables. can we do through one background script. please help.

2 REPLIES 2

merry678gar
Tera Contributor

Hello,

Yes, it is possible to update the affected catalog items and variables using a background script in ServiceNow. You can create a script that iterates through the catalog items and variables, creates the new inactive variables, and then transfers the values of the old variables to these new ones. Below is an example of how you can structure the background script to achieve this:

javascript
// Create the new inactive variables and transfer values from the old variables
(function() {
// Get all catalog items (replace with your filter if needed)
var catalogItems = new GlideRecord('sc_catalog_item');
catalogItems.query();

while (catalogItems.next()) {
// For each catalog item, get the variables (replace with the correct variable table)
var variables = new GlideRecord('sc_item_option');
variables.addQuery('catalog_item', catalogItems.sys_id);
variables.query();

while (variables.next()) {
// Check if the variable has the type change (replace with the appropriate field check)
if (variables.type == 'new_type' && variables.active == true) {
// Create new deactivated variable with the old type (inactive)
var newVar = new GlideRecord('sc_item_option');
newVar.initialize();
newVar.catalog_item = catalogItems.sys_id;
newVar.name = variables.name + '_old'; // Creating a new name, adjust as necessary
newVar.type = 'old_type'; // Use the previous variable type here
newVar.active = false; // Set the new variable as inactive
newVar.insert();

// Transfer the value of the old variable to the new one (as a string)
var oldValue = variables.getValue('value'); // Get the value from the old variable
newVar.value = oldValue; // Set the value to the new deactivated variable
newVar.update();
}
}
}
})();
Explanation:
Catalog Item and Variables Query:

We first query all catalog items using sc_catalog_item and then loop through the catalog items.
For each catalog item, we query the associated variables (sc_item_option).
Check for Variable Type:

Inside the loop, we check if the variable has been updated to the new type (variables.type == 'new_type').
If the variable type has changed and the variable is active, we create a new inactive variable with the old type.
Creating and Updating the New Variable:

We create a new variable (newVar) with the old type (old_type), set it as inactive (active = false), and insert it into the table.
Then, we copy the value from the old variable (variables.getValue('value')) to the new inactive variable.
Best  Regards
merry867

merry678gar
Tera Contributor

Hello,
I hope this info suggestion will hepls you.
Best Regards
Official Site
merry867