Close RITM only if all tasks are closed

sbeginner
Kilo Guru

Hi All,

I have a workflow that creates a single task and other tasks team creates for approvals and stuff. The issue is, once the 

workflow created task gets Closed the WF moves to end activity and stage is completed even when other manual tasks are still open. I tried writing a Before/update After/update BR but is not helping. Can you please help?

1 ACCEPTED SOLUTION

Rana5
Tera Expert

Create a Wait for condition with following script:

 

var task = new GlideRecord("sc_task");
task.addEncodedQuery("stateNOT IN3,4^request_item=" + current.sys_id);
task.query();
var count = task.getRowCount();
if (count == 0)
answer = true;

 

You can put this inside a function as well. No need of any BR or additional field on RITM level.

 

Regards,

Rana

View solution in original post

13 REPLIES 13

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @sbeginner ,

Please create a custom checkbox field on RITM table. Use the same business rule to mark the checkbox as true when all the tasks are completed. Also add a 'wait for condition' activity in the workflow so that whenever the checkbox is checked then only move towards closure of the RITM.

The above configuration can achieve your requirement with minimum efforts.

Please mark my answer as correct if it helps.
BR,
Nayan

If my answer solved your issue, please mark my answer as āœ…Correct & ļ‘Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Hi Nayan, Thanks for your reply. Will try this in next 15 mins and reply back.

Hi @Nayan Dhamane ,

I tested this as you said and it is now not moving to Closure after first task but it is not moving ahead even when all tasks are closed. Can you please have a look at BR and point out what is wrong please. It is a Before/Update BR on sc_req_item table. Also added wait for condition in WF, screenshot belwo:

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

	var taskCatItem = new GlideRecord('sc_task');
    taskCatItem.addActiveQuery();
    taskCatItem.addQuery('request_item', current.getUniqueValue());
    taskCatItem.query();
    if (taskCatItem.hasNext()) {
        //there are still open tasks belonging to requested item, abort update
		gs.log('Task is Open', 'SAP');
		gs.u_all_tasks_closed = 'false';
        gs.addInfoMessage('There is still at least 1 active child task for ' + current.getDisplayValue());
        current.setAbortAction(true);
    }

  

sbeginner_0-1669108125811.pngsbeginner_1-1669108207365.png

 

Hello @sbeginner ,

Please write a new business rule on after ,update on sc_task table and please use the below code in it:

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

var ritm= new GlideRecord('sc_req_item');
ritm.addQuery('sys_id',current.request_item);
ritm.query();
if (ritm.next()){
var taskCatItem = new GlideRecord('sc_task');
taskCatItem.addActiveQuery();
taskCatItem.addQuery('request_item', ritm);
taskCatItem.query();
if (taskCatItem.hasNext()) {
ritm.u_all_tasks_closed='false';
ritm.update();
}
else {
ritm.u_all_tasks_closed ='true';
ritm.update();
}


}})(current, previous);

 

Please mark correct if it helps.
BR,
Nayan

If my answer solved your issue, please mark my answer as āœ…Correct & ļ‘Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.