Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Fix script

DeveloperG
Tera Contributor

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

1 ACCEPTED SOLUTION

Its_Azar
Tera Guru
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

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 

_____
This reply is 100 % GlideFather and 0 % AI

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

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

Bhuvan
Giga 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