Fix script

DeveloperG
Giga Contributor

ServiceNow fix script to close an SCTASK and ensure the associated RITM is updated accordingly using

1 ACCEPTED SOLUTION

Its_Azar
Tera Guru

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.

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

 Microsoft MVP (AI Services), India

View solution in original post

5 REPLIES 5

GlideFather
Tera Patron

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! */


DeveloperG
Giga Contributor

@GlideFather 

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!

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);

 

Bhuvan
Kilo Patron

@DeveloperG 

 

Use below thread to modify your script to close RITM and related tasks,

 

https://www.servicenow.com/community/developer-forum/close-all-ritm-s-its-associated-tasks-using-bac...

 

If this helped to answer your query, please accept the solution and close the thread.

 

Thanks,

Bhuvan