How to get all childelements (JavaDelegate, Camunda BPM Reactor)

Hello Community,

hopefully you can help me. I want to get all child elements (all of the following bpmn elements of the current process definition (type: User Task)) at the position of the current User Task where my UserTaskListener is located and called. How can I do this?
I tried to use the getChildElementsByType() method but there is no result?
I am using the Camunda BPM Reactor and an UserTaskListener which is called when an User Task is completed.

Collection taskChildElementUT = delegateTask.getBpmnModelElementInstance().getChildElementsByType(UserTask.class);
for (UserTask userTask : taskChildElementUT) {
System.out.println("Child elements ‘User Task(s)’: "+ userTask.getName());
}

Or do I need to take another method to get all of the following bpmn elements (type: User Task) after the UserTaskListener has been called?

Thanks a lot!

Regards,
Paul

Hi Paul,

#getChildElementsByType returns all child elements in the XML sense, so it doesn’t help you. You can access all nodes that follow a certain node as follows:

UserTask userTask = modelInstance.getModelElementById("foo");
Collection<SequenceFlow> outgoingSequenceFlows = userTask.getOutgoing();

outgoingSequenceFlows.forEach(f -> {
  FlowNode nextFlowNode = f.getTarget();

  if (nextFlowNode instanceof UserTask)
  {
    // do something
  }
});

You can do this repeatedly to traverse the process graph. Just be aware that you may have to watch out for infinite loops in your process model.

Cheers,
Thorben

Hi Thorben,

thanks a lot for your answer. I will implement an algorithm with your advice above. Looks good and makes sence.

Best regards,
Paul