Background script for cancelling ritm and req in servicenow
- User
Background script for cancelling ritm and req in servicenow
- Assistant
Background script for cancelling ritm and req in servicenow
Creating a script to cancel Requested IT Items (RITMs) and Requests (REQs) in ServiceNow involves using the ServiceNow Script Editor to write server-side JavaScript. This script can be executed from a Background Script module to cancel the specific RITMs and their corresponding REQs. Below is an example of how you might structure such a script.
Please make sure you have the appropriate permissions and understand the impact of running such a script in your ServiceNow instance, especially in a production environment.
(function() {
// Ensure the script is only run by authorized users
if (!gs.hasRole('admin')) {
gs.error('Unauthorized access - you do not have permission to run this script.');
return;
}
// Define the RITM and REQ number to cancel
var ritmNumber = 'RITM1234567'; // Replace with actual RITM number
var reqNumber = 'REQ1234567'; // Replace with actual REQ number
// Cancel the RITM
var ritmGr = new GlideRecord('sc_req_item');
if (ritmGr.get('number', ritmNumber)) {
if (ritmGr.state != 'Closed' && ritmGr.state != 'Canceled') {
ritmGr.setValue('state', '7'); // 7 is the state value for "Canceled"
ritmGr.setWorkflow(false);
ritmGr.update();
gs.log('RITM ' + ritmNumber + ' has been canceled.');
} else {
gs.log('RITM ' + ritmNumber + ' is already closed or canceled.');
}
} else {
gs.error('RITM ' + ritmNumber + ' not found.');
}
// Cancel the REQ
var reqGr = new GlideRecord('sc_request');
if (reqGr.get('number', reqNumber)) {
if (reqGr.state != 'Closed' && reqGr.state != 'Canceled') {
reqGr.setValue('state', '7'); // 7 is the state value for "Canceled"
reqGr.setWorkflow(false);
reqGr.update();
gs.log('REQ ' + reqNumber + ' has been canceled.');
} else {
gs.log('REQ ' + reqNumber + ' is already closed or canceled.');
}
} else {
gs.error('REQ ' + reqNumber + ' not found.');
}
// Cancel all associated catalog tasks for the RITM
var taskGr = new GlideRecord('sc_task');
taskGr.addQuery('request_item', ritmGr.sys_id);
taskGr.query();
while (taskGr.next()) {
if (taskGr.state != 'Closed' && taskGr.state != 'Canceled') {
taskGr.setValue('state', '7'); // 7 is the state value for "Canceled"
taskGr.setWorkflow(false);
taskGr.update();
gs.log('Task ' + taskGr.number + ' associated with RITM ' + ritmNumber + ' has been canceled.');
} else {
gs.log('Task ' + taskGr.number + ' associated with RITM ' + ritmNumber + ' is already closed or canceled.');
}
}
})();
Test this script in a development or test instance before using it in a production environment to ensure it behaves as expected. Adjust the logic as necessary based on your instance's specific business rules and configurations.