Terminate subworkflow in code

I use a sub-workflow with 'wait until finished' cleared to contain optional tasks.  This works fine, but at a downstream point in the main workflow, I want to set the optional tasks as closed and completed in code (not script) if the user chose not to execute these.  My workflow looks something like this:

Start A
Task A1
Sub-workflow B
  Start B
  Task B1
  Task B2
  End B
Task A2
Task A3 <--- At this point I want to set B tasks to complete
..... rest of workflow

Sub-workflow B, Task B1 and Task A2 are created at the same time, which is what I want.  I can catch the point at which I want to terminate Sub-workflow B in the OnTaskCreated event of Task A3.  I have tried changing statuses of Tasks B1 and B2 to their completion statuses in this event.  It works for B1, but B2 has not yet been created at this point.  How can I finish entire Sub-workflow B at this point?


Hello Johannes,

you can't terminate a task that was not even created (B2). When task B1 finishes, it starts task B2. Some creativity has to be used, for example, maybe you could put some condition between task B1 and B2 so that if condition is true, task B2 is skipped (thus not created). So when you finishes B1 from task A3, you could also set such flag and make that condition becomes true to avoid B2 to be created.

Thank you, this works well.  I added a 'skip' completion status to task B1, with a conditional transition to the end block of sub-workflow B.  When I set this status in the OnCreate event of task A3, Task B2 is not created, and entire sub-workflow B finishes.