Workflow

swatis22
Tera Contributor
Under a RITM, three tasks are being created. However, when I close one task, the entire RITM is getting closed. I want the RITM to be closed only after all three tasks are completed. I have already applied a "Wait for Condition" in the workflow, but it is not working as expected.
swatis22_0-1780292786236.png

 

and the script is:

// Get current RITM
var ritm = current;
 
// Check if any related Catalog Tasks are still open
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', ritm.sys_id);
taskGR.addQuery('state', 'NOT IN', '3,4,7'); // Adjust based on your instance (Closed/Cancelled states)
taskGR.query();
 
if (!taskGR.hasNext()) {
    // All tasks are completed → Close RITM
    ritm.state = 3; // Closed Complete (verify state value in your instance)
    ritm.stage = 'complete';
    ritm.update();
 
    // Now close parent REQ
    var reqGR = new GlideRecord('sc_request');
    if (reqGR.get(ritm.request)) {
 
        // Check if any other RITMs under this REQ are still open
        var ritmCheck = new GlideRecord('sc_req_item');
        ritmCheck.addQuery('request', reqGR.sys_id);
        ritmCheck.addQuery('state', 'NOT IN', '3,4,7');
        ritmCheck.query();
 
        if (!ritmCheck.hasNext()) {
            reqGR.state = 3; // Closed Complete
            reqGR.update();
        }
    }
}
 
 
3 REPLIES 3

pr8172510
Tera Guru

Hi @swatis22 ,

You can use a Wait for Condition activity to ensure all associated Catalog Tasks are closed before the workflow proceeds to close the RITM.

var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', current.sys_id);
taskGR.addQuery('active', true); // Checks for any task that is still active (state is 1, 2, or 3)
taskGR.query();

answer = !taskGR.hasNext(); // Returns true only when NO active tasks remain

Placing this logic in a Wait for Condition activity will cause the workflow to pause and re-evaluate whenever a related Catalog Task is updated. The workflow will continue only after all Catalog Tasks for the RITM are no longer active.

Also, verify that there are no custom Business Rules, Flows, or Script Actions on sc_task or sc_req_item that are closing the RITM when the first task is completed.

You can also refer to this ServiceNow Community thread which discusses the same requirement:

https://www.servicenow.com/community/workflow-automation-forum/closing-a-ritm-after-all-tasks-are-cl...

 

Tanushree Maiti
Tera Patron

Hi @swatis22 

 

Instead of a Wait for Condition, the most reliable OOB method in graphical workflows
is using a Join activity:
  1. Drag the 3 separate Catalog Task activities out of your workflow so they run in parallel/ connect them sequentially with a Join at the end.
  2. Connect the Wait or Always condition of all three Catalog Tasks to a single Join activity. 
  3. Connect the output of the Join activity to your "Set Values" or "End" activity.

 


Result: The workflow will pause at the Join until all three tasks output their condition before proceeding to close the RITM

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Ankur Bawiskar
Tera Patron

@swatis22 

my suggestion

-> don't close RITM from your workflow

-> use after update BR on catalog task table and then close RITM once last catalog task is closed

you can use business rule or flow designer for this

BR approach: I shared the script few years ago; check this

How can we close RITM when all the catalog task is closed for that item? 

you can use after update BR on sc_task

Condition: State [IS ONE OF] Closed Complete/Closed Incomplete AND Request Item.Item == Your Catalog Item

Script:

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


        // Add your code here

        var gr = new GlideRecord('sc_task');

        gr.addQuery('request_item', current.request_item);

        gr.addQuery('active', true);

        gr.query();

        if(!gr.next()){

               var ritm = current.request_item.getRefRecord();

               ritm.state = 3;

               ritm.update();

        }


})(current, previous);

Flow Designer approach: I shared solution few years ago

I have shared solution below using flow designer to close parent INC when all child INCs are closed.

Enhance it for sc_req_item and sc_task table

If all child incident is closed then after Parent incident should be closed. How to achieve through....

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader