How to know which element is connected (sequenceflow) to a task

Hello,

Is it possible to get information from the connected tasks?
In this example you see a starter and 2 tasks (1, 2). Is it possible to find out in code which task is connected to task 2?

<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="sample-diagram" targetNamespace="http://bpmn.io/schema/bpmn" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">
  <bpmn2:process id="Process_1" isExecutable="false">
    <bpmn2:startEvent id="StartEvent_1">
      <bpmn2:outgoing>SequenceFlow_1ocbk4v</bpmn2:outgoing>
    </bpmn2:startEvent>
    <bpmn2:task id="Task_1wzwxyu" name="1">
      <bpmn2:incoming>SequenceFlow_1ocbk4v</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_0mizci7</bpmn2:outgoing>
    </bpmn2:task>
    <bpmn2:sequenceFlow id="SequenceFlow_1ocbk4v" sourceRef="StartEvent_1" targetRef="Task_1wzwxyu" />
    <bpmn2:task id="Task_1ycg1p3" name="2">
      <bpmn2:incoming>SequenceFlow_0mizci7</bpmn2:incoming>
    </bpmn2:task>
    <bpmn2:sequenceFlow id="SequenceFlow_0mizci7" sourceRef="Task_1wzwxyu" targetRef="Task_1ycg1p3" />
  </bpmn2:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
        <dc:Bounds x="202" y="52" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Task_1wzwxyu_di" bpmnElement="Task_1wzwxyu">
        <dc:Bounds x="290" y="30" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_1ocbk4v_di" bpmnElement="SequenceFlow_1ocbk4v">
        <di:waypoint x="238" y="70" />
        <di:waypoint x="290" y="70" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="Task_1ycg1p3_di" bpmnElement="Task_1ycg1p3">
        <dc:Bounds x="450" y="30" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_0mizci7_di" bpmnElement="SequenceFlow_0mizci7">
        <di:waypoint x="390" y="70" />
        <di:waypoint x="450" y="70" />
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn2:definitions>

I know I can use the eventBus to get the different events and information from them, but I can’t find where to see if a task is connected and if so from where he is connected to.

Any help is appreciated.

Kind regards

You can find this out by accessing the businessObject of an element

var elementRegistry = modeler.get('elementRegistry'),
  task = elementRegistry.get('Task_1ycg1p3'),
  businessObject = task.businessObject;

var outgoingConnections = businessObject.outgoing;
var incoming = businessObject.incoming;

Which will get you the connections for the task. With them, you could then indirectly get the connected elements with the outgoing and incoming properties once again.

1 Like

Thank you for the second time today Niklas :+1: