Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

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
Kilo Sage

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,
Azar
Serivenow Rising Star
Developer @ KPMG.

View solution in original post

5 REPLIES 5

Its_Azar
Kilo Sage

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,
Azar
Serivenow Rising Star
Developer @ KPMG.