Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

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
Mega 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
Architect@ KPMG.

View solution in original post

5 REPLIES 5

Its_Azar
Mega 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
Architect@ KPMG.