How to map variables from one RITM to another?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2024 05:51 AM
Hello Everyone,
I need to map RITM variables from one RITM to another. For example, on the source RITM, there are "Mode after" (reference) and "Business justification" (string) variables that need to be updated on the target RITM. The target RITM also has the same variables, so I want to map the source RITM variables to the target RITM and display the variables on the target RITM.
How can I achieve this?
I have written a script for this, but it is only mapping the "Business justification" (string) variable and not updating the "Mode after" (reference) variable.
Here is the Business Rule Script:
(function executeRule(current, previous /*null when async*/) {
var sourceItem = new GlideRecord('sc_req_item');
if (sourceItem.get(current.sys_id)) {
gs.log('Source RITM found: ' + sourceItem.sys_id);
// Check if the sap_erp1 variable is true
if (sourceItem.variables.sap_erp1 == 'true') {
gs.log('sap_erp1 is true in source RITM.');
var targetItem = new GlideRecord('sc_req_item');
targetItem.addQuery('request', current.request);
targetItem.addQuery('cat_item', 'a7f5026783d20210f1da9496feaad3d0');
targetItem.query();
while (targetItem.next()) {
gs.log('Target RITM found: ' + targetItem.sys_id);
// Define the variable mapping
var variableMapping = {
'subject1': 'subject1',
'business_justification1': 'business_justification',
'v_model_after_select_not_applicable_when_appropriate2': 'model_after_select_not_applicable_when_appropriate'
};
// Update the target RITM with the specific variable values
for (var sourceVariable in variableMapping) {
var targetVariable = variableMapping[sourceVariable];
var variableValue = sourceItem.variables[sourceVariable].getDisplayValue();
gs.log('Updating ' + targetVariable + ' with value: ' + variableValue);
// Update the target variable value
targetItem.variables[targetVariable] = variableValue;
// Ensure the variable is correctly linked to the target RITM
var itemOption = new GlideRecord('sc_item_option');
itemOption.addQuery('item_option_new.name', targetVariable);
itemOption.query();
if (itemOption.next()) {
gs.log('Item option found for variable: ' + targetVariable);
var targetVariableLink = new GlideRecord('sc_item_option_mtom');
targetVariableLink.addQuery('request_item', targetItem.sys_id);
targetVariableLink.addQuery('sc_item_option', itemOption.sys_id);
targetVariableLink.query();
if (!targetVariableLink.next()) {
targetVariableLink.initialize();
targetVariableLink.request_item = targetItem.sys_id;
targetVariableLink.sc_item_option = itemOption.sys_id;
targetVariableLink.insert();
gs.log('Linked variable ' + targetVariable + ' to target RITM.');
} else {
gs.addInfoMessage('Variable ' + targetVariable + ' already linked to target RITM.');
}
} else {
gs.addInfoMessage('Item option not found for variable: ' + targetVariable);
}
}
// Save the updates to the target RITM
targetItem.update();
}
} else {
gs.log('sap_erp1 is not true in source RITM.');
}
} else {
gs.log('Source RITM not found.');
}
})(current, previous);
Can anyone please help me with this?
Thanks,
Shareef
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2024 07:20 AM
you said it's not working for reference variable; so you need to set sysId and not displayValue there
try this line
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader