- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2025 09:50 AM
ServiceNow fix script to close an SCTASK and ensure the associated RITM is updated accordingly using
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2025 09:53 AM
Hi there @DeveloperG
Try this script
(function executeFixScript() {
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('state', '!=', '3');
taskGR.query();
while (taskGR.next()) {
taskGR.state = 3;
taskGR.work_notes = 'Closed by fix script.';
taskGR.update();
if (taskGR.request_item) {
var ritmGR = taskGR.request_item.getRefRecord();
var allClosed = true;
var checkTasks = new GlideRecord('sc_task');
checkTasks.addQuery('request_item', ritmGR.sys_id);
checkTasks.query();
while (checkTasks.next()) {
if (checkTasks.state != 3) {
allClosed = false;
break;
}
}
if (allClosed) {
ritmGR.stage = 'closed_complete';
ritmGR.work_notes = 'Closed automatically as all tasks are complete.';
ritmGR.update();
}
}
}
})();
If this helps do accept the solution thanks.
Kind Regards,
Mohamed Azarudeen Z
Developer @ KPMG
Microsoft MVP (AI Services), India
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2025 01:38 PM
Hi @DeveloperG
your question is incomplete, can you please review it? There’s nothing to help you at this moment
/* If my response wasn’t a total disaster ↙️ ⭐ drop a Kudos or Accept as Solution ✅ ↘️ Cheers! */
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2025 06:59 AM
Hi,
I have a list of sctask which I want to close by using fix script but using this fix script
var taskGR = new GlideRecord('sc_task');
taskGR.addEncodedQuery('
assignment_group=f8380f211bf00690a7ab20e2b24bcb98^active=true^state=1');
taskGR.query();
while (taskGR.next()) {
taskGR.state = 3;
taskGR.close_notes = 'Closed via script';
taskGR.update();
}
gs.print('Tasks closed: ' + taskGR.getRowCount());
I am using this script to close all the task,task are getting closed but the ritm associated the the sctask is not getting close
Please help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2025 09:11 AM - edited 08-17-2025 09:11 AM
Hi @DeveloperG
May you try this code, I’ve tested from my side and work
// Step 1: Close sc_tasks
var taskGR = new GlideRecord(‘sc_task’);
taskGR.addEncodedQuery(‘assignment_group=f8380f211bf00690a7ab20e2b24bcb98^active=true^state=1’);
taskGR.query();
var ritmSysIds = []; // Store RITM sys_ids to close later
while (taskGR.next()) {
// Store the parent RITM sys_id before closing the task
if (taskGR.request_item && ritmSysIds.indexOf(taskGR.request_item.toString()) == -1) {
ritmSysIds.push(taskGR.request_item.toString());
}
```
taskGR.state = 3; // Closed Complete
taskGR.close_notes = 'Closed via script';
taskGR.update();
```
}
gs.print(’sc_tasks closed: ’ + taskGR.getRowCount());
// Step 2: Close associated RITMs
var closedRITMs = 0;
for (var i = 0; i < ritmSysIds.length; i++) {
var ritmGR = new GlideRecord(‘sc_req_item’);
if (ritmGR.get(ritmSysIds[i])) {
// Check if all sc_tasks for this RITM are closed
var openTasks = new GlideRecord(‘sc_task’);
openTasks.addQuery(‘request_item’, ritmSysIds[i]);
openTasks.addQuery(‘active’, true);
openTasks.query();
```
// If no open tasks remain, close the RITM
if (openTasks.getRowCount() == 0) {
ritmGR.state = 3; // Closed Complete
ritmGR.close_notes = 'Closed via script - all tasks completed';
ritmGR.update();
closedRITMs++;
}
}
```
}
gs.print(’RITMs closed: ’ + closedRITMs);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2025 09:15 AM
Use below thread to modify your script to close RITM and related tasks,
If this helped to answer your query, please accept the solution and close the thread.
Thanks,
Bhuvan