Wait for last task within a Subflow that runs within a For Each to Complete

iDNS
Tera Expert

Hi,

 

I have a Subflow that is setup within a For Each Container that has all multiple tasks based on condiiton. I want to wait for all the tasks withing the subflow to complete to proceed. I cannot use Wait for Subflow execution as it's in a For Each & enabling it will block next task unti the first task is complete. 

 

I have tried 'Do until' running outside the 'For Each' which fails due to maximum execution exceeded. I tried to lookup the Subflow execution to complete with a Looku & Wait for condition for it to complete which is visible but shows Empty when saved. 

 

Any advise how do we wait for all these tasks to be Closed before it comes here. Thanks in advance. 

 

iDNS_0-1750791955317.png

 

3 REPLIES 3

Abbas_5
Tera Sage
Tera Sage

Hello @iDNS,

 

To ensure all tasks within a For Each container's subflow complete before proceeding, consider using a "Do Until" loop with a counter and a combined state checkThis approach avoids the blocking behavior of "Wait for Subflow" within the loop and handles the maximum execution time issue by incrementing a counter and breaking the loop when all tasks are complete.
 
Here's a breakdown of the suggested solution: 
  1. 1. Initialize a Counter:
    Create a flow variable (e.g., task_counter) and initialize it to 0. This will track the number of completed tasks.
  2. 2. "Do Until" Loop:
    Place a "Do Until" loop outside the For Each container.
  3. 3. Inside the "Do Until" Loop:
    • Increment Counter: Inside the loop, increment the task_counter variable by 1 in each iteration.
    • Check Task Completion: Use a "Look Up Records" action to find all the tasks within the current iteration of the For Each loop (based on the For Each's iteration value and any relevant conditions).
    • State Check: For each task found, check its state. If all tasks are in a "closed" state (or your desired completion state), set a flag (e.g., all_tasks_completed to true).
    • Break Condition: If the all_tasks_completed flag is true, use a "Set Flow Variables" action to set the loop's exit condition (e.g., set Do Until loop's condition to true).
  4. 4. Loop Exit:
    The "Do Until" loop will continue until the exit condition is met (i.e., all tasks are closed).
  5. 5. Continue:
    After the "Do Until" loop, the flow can safely proceed to the next task, knowing that all subflow tasks have completed.
By implementing this approach, you ensure that the flow waits for all tasks within the subflow to complete before proceeding, without causing blocking issues within the For Each loop.
Code
graph LR    A[Start] --> B(For Each Container);    B --> C{Subflow Tasks (e.g., Create Catalog Task)};    C -- Iteration --> D{Look Up Records (Tasks)};    D -- Check State --> E{All tasks closed?};    E -- Yes --> F{Set Flow Variable: all_tasks_completed = true};    F --> G(Do Until Loop Exit Condition);    E -- No --> H(Increment task_counter);    H --> C;    G -- Exit --> I(Continue);    I --> J(End);
This diagram illustrates the flow of the suggested solution. It includes the "For Each" container, the subflow tasks, the "Look Up Records" action to check task states, the logic to determine if all tasks are complete, the loop control mechanism (Do Until), and the continuation point after all tasks are closed. 
 
If this is helpful, please hit the thumbs up button and accept the correct solution by referring to this solution in future it will be helpful to them.
 
Thanks & Regards,
Abbas Shaik

I've already tried that, but it doesn’t work as expected. The issue is that the Subflow uses a Wait for Condition step to generate additional tasks. However, these tasks aren’t accounted for because the system moves on to the Do Until loop before those additional tasks—triggered by the wait condition—are created.

Hi @iDNS ,

 

To wait for all tasks created in a subflow to complete, you can either return a list of task sys_ids from the subflow or use a shared identifier like a parent record or some filter. In the main flow, use a Lookup Records action to fetch all tasks—either by filtering on the returned sys_ids or by using the shared identifier (e.g., Parent = current.sys_id). This gives you a list of all tasks created during the subflow execution.

Next, use a For Each loop on the lookup results and add a Wait for Condition inside the loop. This condition should check that each task’s state is “Closed” (or your desired completion state). This setup ensures the flow waits for each task to finish before continuing, without using scripts or hitting execution limits. 

 

Please mark this as helpful and correct if this helps.

 

Thanks,

Yaswanth.