In servicenow, i have a requirement that i need to change one variable on ritm which is read only, and i need to change the value of that particular variable from DR to DC
Assistant
In ServiceNow, if you need to change the value of a read-only variable on a Requested Item (RITM), you can achieve this using a server-side script, such as a script in a Business Rule or a Background Script. Since the variable is read-only, you'll need to override its value programmatically.
User
I need to do it one time, but we cannot use background script, can we able to do it via transform map
Assistant
User
Not working, how to update variable from transform map via transform script
Assistant
User
How to update variable of a ritm via background script
Assistant
Updating a variable of a Requested Item (RITM) via a background script in ServiceNow involves interacting with the and tables, which manage the variable mappings and values for catalog items. Here's a step-by-step guide on how you can update a variable for a specific RITM using a background script:
Here's a step-by-step guide to accomplish this:
Identify the Variable:
First, ensure you have the sys_id of the RITM and the name of the variable you want to change.
Create a Business Rule or Script:
You can create a Business Rule for the table [sc_item_option_mtom] where all RITM variables are stored, or use a Background Script if it's a one-time change.
Script Example:
Here's how you can do it via a Background Script for one-time execution:
javascript
// Replace with your RITM sys_id
var ritmSysId = 'YOUR_RITM_SYS_ID';
// Replace with your variable name
var variableName = 'YOUR_VARIABLE_NAME';
// Query sc_item_option_mtom table for variable related to a specific RITM
var gr = new GlideRecord('sc_item_option_mtom');
gr.addQuery('request_item', ritmSysId);
gr.addQuery('sc_item_option', variableName);
gr.query();
while (gr.next()) {
// Check if the current value is DR and you want to change it to DC
if (gr.value == 'DR') {
gr.value = 'DC';
gr.update();
}
}
Considerations:
Permissions: Ensure the script has the correct permissions and roles to update the variables.
Testing: Always test in a development or test instance before applying changes in production.
Impact: Be aware of any business logic that might use this value. If there are workflow actions or other scripts that trigger based on the variable value, consider the impact of bypassing the read-only restriction.
After-the-fact UI Adjustment:
If users need to see or edit this in the UI afterward, you might also need to temporarily adjust the UI policy that sets the variable to read-only, perform your changes, then revert the UI policy.
By following the above steps, you can change the value of a read-only variable on a RITM in ServiceNow. Always ensure to handle such operations carefully, especially in production environments.
If you need to make this change one-time and you cannot use a background script, you might consider using a Transform Map in combination with an import set. This approach can be a bit unconventional for a one-time change, but it can work if you're constrained by access or permissions and need to avoid directly scripting in the instance. Here's how you could approach it:
Prepare the Import Set Data:
First, prepare a data file (like a CSV or Excel) containing the sys_id of the RITM and the value for the variable you want to change. Make sure your dataset matches the variable's current configuration.
Create an Import Set Table:
Go to the Import Set Table module and create a new table to hold the import data temporarily.
Upload Data:
Upload your prepared data file to the newly created Import Set Table.
Create a Transform Map:
a. Define the Transform Map:
Go to Transform Maps and create a new Transform Map for the Import Set Table targeting the sc_item_option_mtom table, which stores the variable mappings.
b. Map Fields:
Map relevant fields. At a minimum, you’ll need the sys_id of the RITM (request_item) and the variable value (value), and ensure you are targeting the variable correctly with conditions if necessary.
c. Script the Transform:
If you need to conditionally update only certain records (e.g., only those currently set to 'DR'), you can use the Transform Map scripting options. For instance, write a script in the onBefore transform event:
javascript
(function transformRow(source, target, map, log, action) {
// Check if the variable value needs updating
if (source.variable_value == 'DR') {
target.value = 'DC'; // New value
} else {
action = "ignore"; // Ignore any rows not matching
}
})(source, target, map, log, action);
Test and Run the Transform:
Test your import on a few records to ensure that it correctly updates the desired variable.
Once satisfied, transform your entire import set.
Verify the Changes:
After the transform, verify the updates on the related RITMs to ensure they reflect the new value.
Using a Transform Map provides a structured way to make data changes without direct scripting operations. However, ensure to perform this on a test environment first to avoid unintended data corruption or loss. Additionally, work with your ServiceNow admin to ensure proper access and execution permissions during this process.
When updating a variable in a Transform Map using a transform script, you need to ensure that your script correctly targets the right field and object structure for RITM variables. Variables in ServiceNow are often stored in the sc_item_option and sc_item_option_mtom tables, where sc_item_option_mtom serves as a mapping between catalog items and their variables.
To update a variable via a Transform Map script, you can write a script in the onBefore transform script section. Here’s a more detailed walkthrough of writing this script:
Create the Import Set Table:
Your import set should include at a minimum the request_item (RITM sys_id) and the sc_item_option (variable sys_id) and the new desired value.
Transform Map Setup:
Set up a Transform Map from your Import Set Table to the sc_item_option_mtom table.
Script in onBefore Transform Map Script:
Here's a sample script you can use to update the variable value if conditions are met:
javascript
(function transformRow(source, target, map, log, action) {
// Perform a check or mapping logic if required
var variableName = 'YOUR_VARIABLE_NAME'; // Replace with the name of your variable
// Query the sc_item_option_mtom table for the correct record
var gr = new GlideRecord('sc_item_option_mtom');
gr.addQuery('request_item', source.request_item);
gr.query();
while (gr.next()) {
// Check if this is the correct variable
var scOption = gr.sc_item_option.getDisplayValue();
// Assuming the variable name or ID is what you're checking
if (scOption == variableName && gr.value == 'DR') {
gr.value = 'DC'; // Set new value
gr.update(); // Apply update
}
}
// Alternatively, set the target field's value if mapping directly
// target.value = source.new_value; // Assuming direct mapping from import set
})(source, target, map, log, action);
Execution:
Run the import set with this transform map, which should update the required variables matching the specified conditions.
Verify:
Ensure you can verify the records by checking the RITMs to confirm if variables have been updated successfully.
Testing and Backup:
As with any operation modifying data, perform this on a non-production instance first, and ensure you have backups of your data.
This script will execute during the transform process, and is designed to conditionally update variable values for a specific RITM and variable based on import data criteria. If you continue to encounter issues, double-check field mappings, the logic used in your script, and use debugging statements like gs.log() to troubleshoot further.
sc_item_option_mtom
sc_item_option
Determine IDs and Names:
Know the sys_id of the RITM for which you want to update the variable.
Identify the variable name or sys_id that you want to update.
Write the Background Script:
In the script, query the sc_item_option and sc_item_option_mtom tables to find the correct variable associated with the RITM. Then update its value.
Here's an example of how you might structure the script:
javascript
// Replace these values with your specific RITM sys_id and variable name
var ritmSysId = 'YOUR_RITM_SYS_ID';
var targetVariableName = 'YOUR_VARIABLE_NAME';
// Query sc_item_option_mtom to find options related to the specified RITM
var mtomGr = new GlideRecord('sc_item_option_mtom');
mtomGr.addQuery('request_item', ritmSysId);
mtomGr.query();
while (mtomGr.next()) {
// Retrieve the sc_item_option record
var optionGr = mtomGr.sc_item_option.getRefRecord();
// Check if this is the variable you want to update
if (optionGr.name == targetVariableName) {
// Check current value
if (optionGr.value == 'DR') { // Assuming you want to change from 'DR' to 'DC'
optionGr.value = 'DC'; // Set new value
optionGr.update(); // Save the change
// Optionally log the update
gs.log('Variable ' + targetVariableName + ' updated for RITM ' + ritmSysId);
}
break; // Exit loop after updating
}
}
Key Considerations:
Permissions: Ensure your user session has the necessary permissions to execute updates via scripts.
Testing: As always, run this script in a non-production environment to validate its behavior before applying it to production.
Safety Checks: Depending on your use case, add additional checks to confirm you're updating the correct variable.
Backup: Have a data backup plan in case you need to restore information if something goes wrong.
This script directly manipulates records in ServiceNow's database and should be used with caution. Ensure you adhere to your organization's governance around data manipulation and change management processes.