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

@Debasis Pati 

you are using wrong variable name

try this

(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 - FIXED VARIABLE NAME
            var task = new GlideRecord('sc_task');
            task.addQuery('request_item', ritmSysID);  // ← WAS ritmID, NOW ritmSysID
            task.addQuery('active', true);
            task.query();
            var activeTaskCount = task.getRowCount();

            if (activeTaskCount > 0) {
                gs.info('Active tasks remain: ' + activeTaskCount + ' for RITM: ' + ritmSysID);
                return;
            }

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

            // Cascade to REQ if needed
            var reqSysID = grRitm.request + '';
            var grReq = new GlideRecord('sc_request');
            if (grReq.get(reqSysID)) {
                grReq.state = grRitm.state;  // Use 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

Kamva
Giga Guru

Hi @Debasis Pati,

 

I wish to give you a helping hand, but I am not understanding the requirement properly. You are using custom made flow to create two catalog tasks from a RITM? Then you want the RITM to be closed once the tasks have been closed as well?

 

Please clarify a bit, I'll assist you, I need to understand the requirement