RITM must be closed-incomplete if any of its child SCTASKs are Closed-Incomplete

PatlalaShaR
Tera Contributor

Hi Team can anyone help me achieving the requirement below


RITM can be closed-complete if all of its child SCTASKs are a combination of Closed-Complete
RITM must be closed-incomplete if any of its child SCTASKs are Closed-Incomplete
A RITM should not be able to be closed without its child SCTASKs being closed


Below script i have return but its not working for the second requirement and its over riding help me with script and it has to achieve only via script

 

(function executeRule(current, previous /*null when async*/ ) {

    var ritm = new GlideRecord('sc_req_item');
    if (!ritm.get(current.request_item)) {
        gs.info('RITM not found for SCTASK {0}', current.number);
        return;
    }


    var CLOSED_COMPLETE = 3;
    var CLOSED_INCOMPLETE = 4;
    if (ritm.state == CLOSED_COMPLETE || ritm.state == CLOSED_INCOMPLETE) {
        gs.info('RITM {0} already closed, skipping update', ritm.number);
        return;
    }


    var task = new GlideRecord('sc_task');
    task.addQuery('request_item', ritm.sys_id);
    task.query();

    var hasIncomplete = false;
    var allClosed = true;
    var allComplete = true;

    while (task.next()) {
        if (task.active == true) {
            allClosed = false;
        }
        if (task.state == CLOSED_INCOMPLETE) {
            hasIncomplete = true;
        }
        if (task.state != CLOSED_COMPLETE) {
            allComplete = false;
        }
    }


    if (allClosed) {
        if (hasIncomplete) {
            ritm.state = CLOSED_INCOMPLETE;
        } else if (allComplete) {
            ritm.state = CLOSED_COMPLETE;
        }
        ritm.update();
        gs.info('Updated RITM {0} to state {1}', [ritm.number, ritm.state]);
    } else {
        gs.info('RITM {0} not updated: not all tasks closed', ritm.number);
    }
})(current, previous);
 

 

7 REPLIES 7

@PatlalaShaR verify other business rules that trigger on state changes, especially those that set the state to "Closed Complete on sc_req_item table. 

Ankur Bawiskar
Tera Patron
Tera Patron

@PatlalaShaR 

can you try this

(function executeRule(current, previous /* null when async */) {
    var ritmGR = new GlideRecord('sc_req_item');
    if (!ritmGR.get(current.request_item)) {
        gs.info('RITM not found for SCTASK {0}', current.number);
        return;
    }

    var CLOSED_COMPLETE = 3;
    var CLOSED_INCOMPLETE = 4;

    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('request_item', ritmGR.sys_id);
    taskGR.query();

    var totalTasks = 0;
    var closedTasks = 0;
    var hasIncomplete = false;

    while (taskGR.next()) {
        totalTasks++;
        if (!taskGR.active) closedTasks++;
        if (taskGR.state == CLOSED_INCOMPLETE) hasIncomplete = true;
    }

    // Only proceed if ALL tasks are closed (inactive)
    if (totalTasks > 0 && closedTasks === totalTasks) {
        var newState;
        if (hasIncomplete) {
            newState = CLOSED_INCOMPLETE;
        } else {
            // Verify ALL tasks are Closed-Complete
            taskGR.initialize();
            taskGR.addQuery('request_item', ritmGR.sys_id);
            taskGR.addQuery('state', '!=', CLOSED_COMPLETE);
            taskGR.query();
            newState = taskGR.hasNext() ? CLOSED_INCOMPLETE : CLOSED_COMPLETE;
        }

        if (ritmGR.state !== newState) {
            ritmGR.state = newState;
            ritmGR.update();
            gs.info('RITM {0} updated to state {1}', [ritmGR.number, ritmGR.state.getDisplayValue()]);
        }
    } else {
        gs.info('RITM {0} not updated: {1}/{2} tasks closed', [ritmGR.number, closedTasks, totalTasks]);
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

HI Ankur, Thanks for response but its still does the same as i mentioned above its over-riding the flow