BPMN IO Replace Element ID Tracking

I have an issue when trying to keep track of the BPMN elements added/removed in the diagram, specifically when using the Replace feature. Below is a description of the steps I’ve taken to reproduce the issue I’m having. Any help is much appreciated.

Drag a Task onto the designer

  1. Fires the shape.added event
    ***ID of element (Task) being added is “Task_1cgl3oq”
    ***I get basic info about the shape (including its ID) and record Task element in array of BPMN elements

Click on the Replace Context Menu Item of the Task (i.e. the Wrench icon), and change the Task to a Send Task

  1. Fires the shape.added event
    ***ID of element (Send Task) being added is “SendTask_126ged2"
    ***I get basic info about the shape (including its ID) and record Send Task element in array of BPMN elements based on the ID
    ***My array now has two elements “Task_1cgl3oq” and “SendTask_126ged2”
  2. Fires the shape.removed event
    ***ID of element (Task) being removed is “Task_1cgl3oq”
    ***I remove the corresponding element from my array of BPMN elements
    ***My array now has one element “SendTask_126ged2”

Click on the Replace Context Menu Item of the Send Task (i.e. the Wrench icon), and change the Send Task to a Receive Task

  1. Fires the shape.added event
    ***ID of element (Receive Task) being added is “ReceiveTask_1u3a0h2”
    ***I get basic info about the shape (including its ID) and record Receive Task element in array of BPMN elements based on the ID
    ***My array now has two elements “SendTask_126ged2” and “ReceiveTask_1u3a0h2”
  2. Fires the shape.removed event
    ***ID of element (Send Task) being removed is “Task_1cgl3oq”
    ***My code fails because there is no element with ID “Task_1cgl3oq” in my array

It seems that after the first Replace, after the shape.added and shape.removed code has finished running that the new Send Task has its ID changed back to the ID of the Task that was removed.

Hey,

you are right! When changing the type of a task, a new task is created and properties (including the ID) are copied to the new task, then the old task gets removed. This is intended behavior, because we want to keep the ID when only the task type is changing. You can keep track of this ID changing by hooking into the eventBus like that:

eventBus.on([ 'commandStack.element.updateProperties.execute' ], (event) => {
  // The newly created element, which has a temporary ID
  var element = event.context.element;

  // properties.id is the old ID which will be copied to the new element
  var properties = event.context.properties;
  if (properties.id) {
    // every time the id is changing
    // do your stuff
  }
});

Does that help?

1 Like

That worked perfectly. Thanks so much.