Copy variables of one RITM to multi row variables set of another RITM.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2025 02:20 AM
Hi All,
In RITM we have one UI action if we click that UI action on RITM (eg: Create New RITM) it should update current RITM and its related sctask state as Closed complete. At the same time new different catalog item should submitted, new Request and RITM should be created. we want to copy variable values from old closed RITM to new RITM variables which are belongs to MULTI ROW VARIABLE SET. Is it possible to copy variable values from one RITM to another RITM multi row variable set variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2025 05:14 AM
Hello,
Yes, this is absolutely possible within ServiceNow using a combination of GlideRecord queries, script includes (for better code organization and reusability), and client-side scripting (if needed for UI interactions). Let's break down the solution into steps:
1. UI Action (Server-Side Script):
Trigger: The UI Action "Create New RITM" on the original RITM.
Functionality:
Close the current RITM and its associated SCTASKs.
Create a new RITM from the desired catalog item.
Copy the multi-row variable set values from the old RITM to the new RITM.
JavaScript
// UI Action Script - Server Side
(function execute() {
var ritmGR = new GlideRecord('sc_req_item');
if (ritmGR.get(current.sys_id)) {
// 1. Close the current RITM and its SCTasks
ritmGR.state = 3; // Closed Complete
ritmGR.update();
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', current.sys_id);
taskGR.query();
while (taskGR.next()) {
taskGR.state = 3; // Closed Complete
taskGR.update();
}
// 2. Create the new RITM
var cart = new Cart();
var item = cart.addItem('YOUR_CATALOG_ITEM_SYS_ID'); // Replace with the new catalog item sys_id
cart.setVariable(item, 'variable_to_set', 'value'); //set needed variables.
var rc = cart.placeOrder();
var newRitmGR = new GlideRecord('sc_req_item');
newRitmGR.addQuery('request', rc.sys_id);
newRitmGR.query();
if(newRitmGR.next()){
// 3. Copy multi-row variable set values
copyMultiRowVariables(current.sys_id, newRitmGR.sys_id, 'YOUR_MULTI_ROW_VARIABLE_SET_NAME'); // Replace with the multi-row variable set name.
}
}
})();
function copyMultiRowVariables(sourceRITM, targetRITM, multiRowVariableName) {
var sourceRitmGR = new GlideRecord('sc_req_item');
if(sourceRitmGR.get(sourceRITM)){
var sourceMultiRow = JSON.parse(sourceRitmGR.variables[multiRowVariableName]);
var targetRitmGR = new GlideRecord('sc_req_item');
if(targetRitmGR.get(targetRITM)){
targetRitmGR.variables[multiRowVariableName] = JSON.stringify(sourceMultiRow);
targetRitmGR.update();
}
}
}
Important Notes:
Replace Placeholders:
YOUR_CATALOG_ITEM_SYS_ID: Replace with the sys_id of the catalog item you want to create.
YOUR_MULTI_ROW_VARIABLE_SET_NAME: Replace with the name of the multi-row variable set.
Error Handling: Add robust error handling (try-catch blocks, logging) to the script to ensure it functions correctly in various scenarios.
Variable Names: Ensure the variable names in the multi-row variable set are identical between the source and target RITMs.
Security: Implement appropriate security measures to prevent unauthorized access and modification of data.
Testing: Thoroughly test the UI Action and script in a non-production environment before deploying it to production.
Multi row variable set: The multi row variable set is stored as a JSON string within the variables object of the RITM.
Best Regards
edwin678