Get Extension Properties - Helper Script

Been asked a few times about Extensions on elements.

You generally have to parse the xml to get the result elements.

So i threw together a little helper script for this:

function getExtensions(elementId, returnJson) {

  var modelInstance = execution.getBpmnModelInstance()
  var elementInstance = modelInstance.getModelElementById(elementId)
  var extensionElements = elementInstance.getExtensionElements().getElementsQuery().filterByType(Java.type('org.camunda.bpm.model.bpmn.instance.camunda.CamundaProperties').class).singleResult().getCamundaProperties().toArray()

  if (returnJson == true){
    return buildJson(extensionElements)
  } else {
    return extensionElements;
  }

  function buildJson(extensionCollection){
    var extArray = []
    for each (var i in extensionCollection) {
      var key = i.getCamundaName()
      var extensionObject = {}
      extensionObject[i.getCamundaName().toString()] = i.getCamundaValue(),
      extArray.push(extensionObject)
    }
    return extArray
  }
}

// Returns as a JSArray of Objects
var extensionsAsJson = getExtensions('SequenceFlow_0p7c9w1', true)

// Use Camunda SPIN and JSON.Stringify() to covert the JSArray into a Camunda SPIN Object
execution.setVariable('extensions', S(JSON.stringify(extensionsAsJson)))

// Returns as a Array of 'org.camunda.bpm.model.bpmn.impl.instance.camunda.CamundaPropertyImpl'
// var extensions1 = getExtensions('SequenceFlow_0p7c9w1', false)
// var extensions2 = getExtensions('SequenceFlow_0p7c9w1')

You can call getExtensions(ActivityId) (The Id field in the General Tab in Camunda Modeler) and it will return the extensions as a Javascript Array of Objects (Because the same Key can be used multiple times in extensions).

Enjoy

x-ref: Extension elements, Access custom extension elements with the BPMN Model API, Get input parameter content with model api - #3 by Gabor_Sandor, Configuring connector input/output map with a variable on parseListener, Extension properties

6 Likes