How to delete element "bpmn:Message" using modeling?

How to delete message element (bpm:Message) using modeling?

The following code throw error:

 var modeling = bpmJS.get('modeling');

modeling.removeElements([message]);

Error:

TypeError: Cannot read property 'id' of undefined
    at bpmn-modeler.min.js:formatted:3876

Hi,

Looks like your message object is invalid element. Please check if you are actually passing existing element.
Where and how do you get the element from?

Vladimir

The error ocurres because the object of element message doesn’t have the businessObject property.

Code to get elements:

var definitions = bpmnJS.get('canvas').getRootElement().businessObject.$parent; var messages = (definitions.rootElements || []).filter(function (element) { return element.$type === 'bpmn:Message'; });

Is there other way?

You should use elementRegistry to get right elements from the diagram:

var is = require('bpmn-js/lib/util/ModelUtil').is;
var messages = modeler.get('elementRegistry').filter(function(element) {
  console.log(element);
  console.log(is(element, 'bpmn:Message'), is(element, 'bpmn:Activity'));
  return is(element, 'bpmn:Message');
});

Cheers,
Vlad

ElementRegistry doesn’t return bpmn:Message elements.

Could you give an example diagram?

`

<?xml version="1.0" encoding="UTF-8"?> SequenceFlow_0zux2gp SequenceFlow_0n6tqjg SequenceFlow_0zux2gp SequenceFlow_0n6tqjg DataObjectReference_0bizxaw `

Hi,

You are looking for bpmn:DataObjectReference rather than ‘bpmn:Message’.

Cheers,
Vlad

Sorry for misunderstanding.
For getting underlying Message element you could do something like this:

var is = require('bpmn-js/lib/util/ModelUtil').is;
var getBusinessObject = require('bpmn-js/lib/util/ModelUtil').getBusinessObject;

var messages = modeler.get('elementRegistry').filter(function(element) {
  var businessObject = getBusinessObject(element);
  var eventDefinition = businessObject.eventDefinitions && businessObject.eventDefinitions[0];

  return is(element, 'bpmn:BoundaryEvent') &&
      !element.labelTarget &&
      eventDefinition.$type === 'bpmn:MessageEventDefinition';
});

messages = messages.map(function(m) {
  return getBusinessObject(m).eventDefinitions[0].messageRef;
});

console.log(messages);

Cheers,
Vladimir

Ok @vladimirs.katusenoks its work only if the process has elements of type bpmn:BoundaryEvent, but if it hasn’t bpmn:BoundaryEvent, the elements of type bpmn:message are still in the xml.

I’m developing a funcionality to allow the user to remove the messages of the process from a list of messages.

What you could do is something like this, as bpmn:Message is a hidden element and not being registered with element factory (so cannot be removed using modeling module, which relies on it):

var definitions = modeler.get('canvas').getRootElement().businessObject.$parent;
var rootElements = definitions.rootElements || [];

var messages = rootElements.filter(function(element) {
  return element.$type === 'bpmn:Message';
});

messages.forEach(function(m) {
  rootElements.splice(rootElements.indexOf(m), 1);
});

PR’s are always welcome for having core support for bpmn:Message type of elements.

Cheers,
Vladimir

Thanks @vladimirs.katusenoks, works!