# Create new processInstances at runtime with parent/child relationship

**URL:** https://forum.flowable.org/t/create-new-processinstances-at-runtime-with-parent-child-relationship/877
**Category:** Flowable Engine
**Created:** [August 21, 2017, 2:27pm UTC](https://forum.flowable.org/t/create-new-processinstances-at-runtime-with-parent-child-relationship/877 "2017-08-21T14:27:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![donkon](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@donkon](https://forum.flowable.org/u/donkon)
#### Post date: [August 21, 2017, 2:27pm UTC](https://forum.flowable.org/t/create-new-processinstances-at-runtime-with-parent-child-relationship/877/1 "2017-08-21T14:27:30Z")

</div>

Hi,

I have to model a typical order - orderItem scenario in activiti:

A process instance is started with some Order Object Variable - containing a list of orderItems. Each orderItem should be handled in its own ProcessInstance.

So I started to model a Parent ProcessDefinition with a MultiInstance CallActivity, which calls the Child ProcessDefinition containing the workflow logic for the OrderItem, but this only starts an execution for each orderItem - not a new ProcessInstance (for us, the problem with executions is, that they can not be monitored by our system).

Is it possible to create new Child ProcessInstances at runtime and keep the parent/child relationship (so that I can query the child process instances with the superProcessInstanceId?

I also have the requirement, that the Parent ProcessInstance should do some final Logic, wenn _all_ Child Process are finished.

---

<div class="post-metadata">

### Author: ![joram](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/joram/32/26_2.png) [@joram](https://forum.flowable.org/u/joram)
#### Post date: [August 22, 2017, 10:43am UTC](https://forum.flowable.org/t/create-new-processinstances-at-runtime-with-parent-child-relationship/877/2 "2017-08-22T10:43:30Z")

</div>

> [@donkon](#):
>
> Is it possible to create new Child ProcessInstances at runtime and keep the parent/child relationship (so that I can query the child process instances with the superProcessInstanceId?

That should be how it is, when using a callactivity. Do note that a process instance is an execution too.  
What query are you using to retrieve the process instances?

> [@donkon](#):
>
> I also have the requirement, that the Parent ProcessInstance should do some final Logic, wenn all Child Process are finished.

The default behaviour of the multi instance is to wait for all the instances (can be tweaked by changing the completion condition). Whatever is after the multi instance, will be done when these instances have finished.

---

<div class="post-metadata">

### Author: ![donkon](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@donkon](https://forum.flowable.org/u/donkon)
#### Post date: [August 23, 2017, 9:44am UTC](https://forum.flowable.org/t/create-new-processinstances-at-runtime-with-parent-child-relationship/877/3 "2017-08-23T09:44:57Z")

</div>

When using the callactivity, I find the “subprocess instances” via

`List<Execution> list = this.getRuntimeService().createExecutionQuery().processInstanceId(processInstanceParentId).list();`

This list contains various Executions, the subprocess executions but perhaps also more executions (e.g. the subprocess itself can also have more executions or the parent process could also have more Executions). So with this query, I can not find out, what my subprocess instances are.

What I want is to find the “subprocess instances” as ProcessInstances via

`List<HistoricProcessInstance> list = this.getHistoryService().createHistoricProcessInstanceQuery().superProcessInstanceId(processInstanceParentId).list();`

Here, I get an empty List.  
In what case do I find HistoricProcessInstances when I filter for ‘superProcessInstanceId’?

---

<div class="post-metadata">

### Author: ![joram](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/joram/32/26_2.png) [@joram](https://forum.flowable.org/u/joram)
#### Post date: [August 29, 2017, 12:15pm UTC](https://forum.flowable.org/t/create-new-processinstances-at-runtime-with-parent-child-relationship/877/4 "2017-08-29T12:15:16Z")

</div>

> [@donkon](#):
>
> So with this query, I can not find out, what my subprocess instances are.

A subprocess will have a superProcessInstanceId, other executions won’t.

> [@donkon](#):
>
> In what case do I find HistoricProcessInstances when I filter for ‘superProcessInstanceId’?

That should work. We’ve got unit tests that verify this (see for example [flowable-engine/modules/flowable-engine/src/test/java/org/flowable/engine/test/api/history/HistoryServiceTest.java at 6bfadc02465f0b5e95aa61390b10f6fbe4759e7c · flowable/flowable-engine · GitHub](https://github.com/flowable/flowable-engine/blob/6bfadc02465f0b5e95aa61390b10f6fbe4759e7c/modules/flowable-engine/src/test/java/org/flowable/engine/test/api/history/HistoryServiceTest.java#L133)). So not sure what’s going wrong here … do you see in the database that the super process instance id has the correct value?
