After Business Rule is getting Executed before my flow is creating Task and closes the RITM

Debasis Pati
Tera Guru

Hello,
This is my After update BR on SC Task Table

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



if (current.request.state != '5') {
var ritmSysID = current.request_item.toString();
var grRitm = new GlideRecord('sc_req_item');
grRitm.query('sys_id', ritmSysID);
grRitm.query();
if (grRitm.next()) {
//Update RITM State
if (current.state == 2) { //Work in Progress
grRitm.state = 2;
grRitm.update();
} else if (current.state == 3 || current.state == 4) {
gs.sleep(10000);

if (grRitm.stage != 'waiting_for_approval') {
var sc_task = new GlideRecord('sc_task');
sc_task.addQuery('request_item', ritmSysID);
sc_task.addQuery('active', true);
sc_task.query();
if (!sc_task.next()) {

if (current.state == 3) { //Closed Complete
grRitm.state = 3;
} else if (current.state == 4) { //Closed Incomplete
grRitm.state = 4;
}

grRitm.update();

var reqSysID = grRitm.request.toString();
var grReq = new GlideRecord('sc_request');
grReq.query('sys_id', reqSysID);
grReq.query();
if (grReq.next()) {
if (grRitm.state == 3) { //Closed Complete
grReq.state = 3;
} else if (grRitm.state == 4) { //Closed Incomplete
grReq.state = 4;
}

grReq.update();
}
}
}
}
}
}
})(current, previous);
My flow is responsible for 2 Task and updating them for automating the state and stages we have created the above BR.
This works fine when i put a delay but as this is not recommended what can be done.
The issue is whenever my first task is gets closed this br runs before the flow creates the other task and RITM value is setting to close complete.
@Ankur Bawiskar any suggestions please?

6 REPLIES 6

Ankur Bawiskar
Tera Patron

@Debasis Pati 

you can check if there are 0 active tasks before closing RITM

-> Ensure BR order is 100

-> Condition: state CHANGES AND in3,4 ^ request.state!=5

-> Flow: Add Wait for Condition in Flow: Number of active tasks = 0

(function executeRule(current, previous /*null when async*/) {
    // Skip if request already closed
    if (current.request.state == '5') return;
    
    var ritmSysID = current.request_item + ''; // Stringify sys_id
    var grRitm = new GlideRecord('sc_req_item');
    if (!grRitm.get(ritmSysID)) return;
    
    // WIP: Set immediately (safe)
    if (current.state == 2) {
        grRitm.state = 2;
        grRitm.update();
        return;
    }
    
    // Closed states (3=Complete, 4=Incomplete): Check ALL tasks first
    if (current.state == 3 || current.state == 4) {
        // Count ACTIVE tasks for this RITM
        var activeTaskCount = countActiveTasks(ritmSysID);
        
        if (activeTaskCount > 0) {
            // Other tasks still open - do nothing
            gs.debug('Active tasks remain: ' + activeTaskCount);
            return;
        }
        
        // No active tasks - safe to close
        grRitm.state = (current.state == 3) ? 3 : 4;
        grRitm.update();
        
        // Cascade to REQ if needed
        cascadeToRequest(grRitm);
    }
    
    function countActiveTasks(ritmID) {
        var task = new GlideRecord('sc_task');
        task.addQuery('request_item', ritmID);
        task.addQuery('active', true);
        task.query();
        return task.getRowCount();
    }
    
    function cascadeToRequest(ritm) {
        var reqSysID = ritm.request + '';
        var grReq = new GlideRecord('sc_request');
        if (grReq.get(reqSysID)) {
            grReq.state = ritm.state;
            grReq.update();
        }
    }
})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hello @Ankur Bawiskar ,
No change

countActiveTasks

the below warning is coming om.glide.script.RhinoEcmaError: "countActiveTasks" is not defined.
  
     

@Debasis Pati 

try this

(function executeRule(current, previous /*null when async*/ ) {
    // Skip if request already closed
    if (current.request.state == '5') return;

    var ritmSysID = current.request_item + ''; // Stringify sys_id
    var grRitm = new GlideRecord('sc_req_item');
    if (!grRitm.get(ritmSysID)) return;

    // WIP: Set immediately (safe)
    if (current.state == 2) {
        grRitm.state = 2;
        grRitm.update();
        return;
    }

    // Closed states (3=Complete, 4=Incomplete): Check ALL tasks first
    if (current.state == 3 || current.state == 4) {
        // Count ACTIVE tasks for this RITM

        var task = new GlideRecord('sc_task');
        task.addQuery('request_item', ritmID);
        task.addQuery('active', true);
        task.query();
        var activeTaskCount = task.getRowCount();

        if (activeTaskCount > 0) {
            // Other tasks still open - do nothing
            gs.debug('Active tasks remain: ' + activeTaskCount);
            return;
        }

        // No active tasks - safe to close
        grRitm.state = (current.state == 3) ? 3 : 4;
        grRitm.update();

        // Cascade to REQ if needed
        var reqSysID = ritm.request + '';
        var grReq = new GlideRecord('sc_request');
        if (grReq.get(reqSysID)) {
            grReq.state = ritm.state;
            grReq.update();
        }
    }

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hello @Ankur Bawiskar ,

i tried the below

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

 

 

 

    if (current.request.state != '5') {

        var ritmSysID = current.request_item + ''; // Stringify sys_id

        var grRitm = new GlideRecord('sc_req_item');

        if (!grRitm.get(ritmSysID)) return;

 

        // WIP: Set immediately (safe)

        if (current.state == 2) {

            grRitm.state = 2;

            grRitm.update();

            return;

        }

 

        // Closed states (3=Complete, 4=Incomplete): Check ALL tasks first

        if (current.state == 3 || current.state == 4) {

            // Count ACTIVE tasks for this RITM

 

            var task = new GlideRecord('sc_task');

            task.addQuery('request_item', ritmID);

            task.addQuery('active', true);

            task.query();

            var activeTaskCount = task.getRowCount();

 

            if (activeTaskCount > 0) {

                // Other tasks still open - do nothing

                gs.info('Active tasks remain: ' + activeTaskCount);

                return;

            }

 

            // No active tasks - safe to close

            grRitm.state = (current.state == 3) ? 3 : 4;

            grRitm.update();

 

            // Cascade to REQ if needed

            var reqSysID = ritm.request + '';

            var grReq = new GlideRecord('sc_request');

            if (grReq.get(reqSysID)) {

                grReq.state = ritm.state;

                grReq.update();

            }

        }

 

 

    }

})(current, previous);


now it is not closing the ritm after closing all the tasks.