Fix script to Cancel the RITMs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2024 05:41 AM
Hello Everyone,
Can you help me with the fix script to cancel the RITMs which are older than 1 year with below condition.
Condition : if any of the Open RITMs with Opened or Closed Approvals (sysapproval_approver) and no Catalog Task.
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2024 05:46 AM - edited 08-27-2024 05:48 AM
@Rakesh40 See if the following works for you.
// Get the date one year ago from today
var oneYearAgo = new GlideDateTime();
oneYearAgo.addYears(-1);
// Query for RITMs (sc_req_item) older than one year
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('sys_created_on', '<=', oneYearAgo);
ritmGR.addQuery('active', true); // Only consider open RITMs
ritmGR.query();
while (ritmGR.next()) {
// Check for associated Catalog Tasks
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', ritmGR.sys_id);
taskGR.query();
if (!taskGR.hasNext()) {
// Check for Approvals (sysapproval_approver) in Opened or Closed state
var approvalGR = new GlideRecord('sysapproval_approver');
approvalGR.addQuery('sysapproval', ritmGR.sys_id);
approvalGR.addQuery('state', 'IN', 'requested,approved,rejected');
approvalGR.query();
if (approvalGR.hasNext()) {
// Cancel the RITM
ritmGR.setValue('state', 'closed_cancelled');
ritmGR.setValue('close_code', 'Cancelled');
ritmGR.setValue('close_notes', 'RITM cancelled due to inactivity and no associated catalog tasks.');
ritmGR.update();
gs.info('RITM ' + ritmGR.number + ' has been cancelled.');
}
}
}
Caution: Please test this script in your dev/sandbox environment first and if the result looks good then run on production.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2024 06:26 AM
Hi @Rakesh40
The below screenshot will help I ran the code and its working as expected :-
--------Script----------
// Get the date one year ago from today
var oneYearAgo = new GlideDateTime();
oneYearAgo.addYears(-1);
// Query for RITMs (sc_req_item) older than one year
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('sys_created_on', '<=', oneYearAgo);
ritmGR.addQuery('active', true);
ritmGR.addQuery('request.state', '=', 'open');
ritmGR.query();
while (ritmGR.next()) {
// Check for associated Catalog Tasks
var hasCatalogTask = new GlideRecord('sc_task');
hasCatalogTask.addQuery('request_item', ritmGR.sys_id);
hasCatalogTask.query();
if (!hasCatalogTask.hasNext()) {
// Check for Approvals (sysapproval_approver) in Opened or Closed state
var hasApproval = new GlideRecord('sysapproval_approver');
hasApproval.addQuery('document_id', ritmGR.sys_id);
hasApproval.addQuery('state', 'IN', 'requested,approved,rejected');
hasApproval.query();
if (hasApproval.hasNext()) {
// Cancel the RITM
ritmGR.setValue('request.state', 'cancelled');
ritmGR.setValue('close_code', 'Cancelled');
ritmGR.setValue('close_notes', 'RITM cancelled due to inactivity and no associated catalog tasks.');
ritmGR.update();
gs.info('RITM ' + ritmGR.number + ' has been cancelled.');
}
}
}
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2024 08:21 AM
Hello @Ravi Gaurav
I want to Cancel the Request with RITM How can i include this condition here ?
and also If i do have Catalog Task Present for RITMs i want to set the Catalog task, RITM and REQ to Closed Automatically.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2024 08:20 AM
Hello @Sandeep Rajput
I want to Cancel the Request How can i include this condition here ?
and also If i do have Catalog Task Present for RITMs i want to set the Catalog task, RITM and REQ to Closed Automatically.