Fix Script to close Parent Request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hello,
Appreciate all your response and help.
We have about 2K Requests which are NOT in "closed complete" state, while their child Requested Items(RITMs) are in closed complete state. The parents requests sit in Approved state.
The fix script below is timing out when ever I run it.
Can someone please help with the Fix script to close the Parent Request items, where its requested items are in closed complete state.
var oneYearAgo = new GlideDateTime();
oneYearAgo.addYears(-3);
// Query the RITM records that are active and not updated for over a year
var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addQuery('active', true);
ritmGr.addQuery('sys_updated_on', '<', oneYearAgo);
ritmGr.query();
while (ritmGr.next()) {
// Close the RITM record
ritmGr.state = 'closed_complete'; // or the appropriate closed state value
ritmGr.update();
// Get associated REQUEST record and close it
var requestGr = new GlideRecord('sc_request');
if (requestGr.get(ritmGr.request)) {
requestGr.state = 'closed_complete'; // or the appropriate closed state value
requestGr.update();
}
// Close associated SCTASK records
var sctaskGr = new GlideRecord('sc_task');
sctaskGr.addQuery('request_item', ritmGr.sys_id);
sctaskGr.addQuery('state', '!=', 'closed_complete'); // Only update tasks that are not already closed
sctaskGr.query();
while (sctaskGr.next()) {
sctaskGr.state = 'closed_complete'; // or the appropriate closed state value
sctaskGr.update();
}
}
gs.info('RITMs, Requests, and SCTasks have been closed successfully.');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi @Shree Nag
I'm not a developer, but a better approach would be to create a flow, use a lookup action to get the record, and then update the state on the REQ.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago - last edited a month ago
Hi @Shree Nag ,
Check the below code:
(function() {
var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addActiveQuery();
ritmGr.addQuery('state', '=', 3);
ritmGr.query();
while (ritmGr.next()) {
var taskGr = new GlideRecord('sc_task');
taskGr.addQuery('request_item', ritmGr.sys_id);
taskGr.addQuery('active', true);
taskGr.query();
if (!taskGr.next()) {
ritmGr.setWorkflow(false);
ritmGr.state = 3;
ritmGr.active = false;
ritmGr.update();
var reqGr = new GlideRecord('sc_req_item');
reqGr.addQuery('request', ritmGr.request);
reqGr.addQuery('state', '!=', 3);
reqGr.query();
if (!reqGr.next()) {
var request = new GlideRecord('sc_request');
if (request.get(ritmGr.request)) {
request.setWorkflow(false);
request.state = 3;
request.update();
}
}
}
}
gs.info('Hello Shree, Parent Requests and RITMs updated.');
})();
For more check the below community link: https://www.servicenow.com/community/itsm-forum/close-parent-request-if-ritm-has-complete-and-cancel...
Try this once and let me know. If you find this helpful, please accept this as a solution and hit the helpful button..
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Thank you for the response,
The script still timed out.
Any other thoughts would be helpful..
-thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi there @Shree Nag
Your original script times out because it's looping over RITMs and repeatedly updating the same Requests. Instead, reverse the logic: loop through Requests, check their RITMs' states, and only update
var count = 0;
var reqGR = new GlideRecord('sc_request');
reqGR.addQuery('state', '!=', 'closed_complete');
reqGR.query();
while (reqGR.next()) {
var allRitmClosed = true;
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('request', reqGR.sys_id);
ritmGR.query();
while (ritmGR.next()) {
if (ritmGR.state != '3') {
allRitmClosed = false;
break;
}
}
if (allRitmClosed) {
reqGR.state = '3';
reqGR.update();
count++;
}
}
gs.info('Total parent requests closed: ' + count);
If this helps kindly accept the solution thanks.
Kind Regards,
Mohamed Azarudeen Z
Developer @ KPMG
ļ Microsoft MVP (AI Services), India