Setting default attribute for elements

Hello,

I’m using bpmn-js-seed-master project. I have the following question: How to define default values for properties of elements (e.g when I add User Task element, it has to contain the attibute: camunda:class=“org.camunda.bpm.getstarted.loanapproval.ProcessRequestDelegate”).

Any hint or guidelines will be very helpful,

Thanks :slight_smile:

You cannot easily do this right now.

You cannot easily do this right now.

But is it possible? Even if it isn’t an easy thing to do.
I need to have a predefined class in service task - every service task will have that class, so would be stupid to make the user select/insert a class value in properties panel.

I’ve defined the properties panel to show a default value but that won’t be written to the xml unless the user modifies it.

I need this for that use case and for others like a selectbox:
Imagine the select box has 2 values (A and B) and A is the one selected by default (i dont have an empty value therefore A will be shown as selected) but that won’t pass to the xml unless the user selects B and then selects A. What it is meant to do is to write A in the xml, even without any input from the user.

Is that possible?

In my descriptor (i’ve made one for activiti) i have this:

[...]
{
      "name": "AnotherTask",
      "extends": [
          "bpmn:ServiceTask"
        ],
      "properties": [
        {
          "name": "test",
          "isAttr": true,
          "type": "String"
        },
        {
          "name": "class",
          "isAttr": true,
          "type": "String",
          "default":"com.company.bpmn.AnotherTask"
        }
      ]
    }
[...]

Then i have a file named AnotherTaskProp.js, in which i defined the the entryfactories for these properties:

function ensureFormKeyAndDataSupported(element) {
    return is(element, 'bpmn:ServiceTask');
}
module.exports = function(group, element, translate) {
    if(!ensureFormKeyAndDataSupported(element)){
        return;
    }

    console.log(getBusinessObject(element));
    group.entries.push(entryFactory.selectBox({
        id: 'test-type',
        label: translate('Test Type'),
        modelProperty: 'testName',
        get: function(element, node) {
            var bo = getBusinessObject(element);
            return {
                'testName' : bo.get('activiti:test') || 'A'
            };
        },
        set: function(element, values, node) {
            var bo = getBusinessObject(element);
            var testName = values.testName;

            return cmdHelper.updateBusinessObject(element, bo, { 'activiti:test': testName});
        },
        selectOptions: [{ name: 'A', value: 'A' }, { name: 'B', value: 'B' }]
    }));


    group.entries.push(entryFactory.textField({
        id: 'class-task',
        label: translate('Class'),
        modelProperty: 'class',
        set: function(element, values, node) {
            var bo = getBusinessObject(element);
            var prop = {};
            prop["activiti:class"] = values.class;

            return cmdHelper.updateBusinessObject(element, bo, prop);
        },
        get: function(element) {
            var bo = getBusinessObject(element);
            cmdHelper.updateBusinessObject(element, bo, {"activiti:class" : getBusinessObject(element).get('activiti:class') || "com.company.bpmn.AnotherTask"});
            return {'class':getBusinessObject(element).get('activiti:class') || "com.company.bpmn.AnotherTask"};
        }

    }));
};

And the values in the “|| other” or "|| ‘com.company.bpmn.AnotherTask’ ", appear in the properties panel but aren’t passed to the XML and even after hours exploring this i haven’t figured how to do it.

You’d need to hook into element creation via the eventBus:

function createPlugin(eventBus) {

  eventBus.on('commandStack.shape.create.postExecute', function(event) {
    // inspect the event; you'll find the created element
    // you can fiddle around with (i.e. to add additional properties)
  });

}

[Edit: it worked, thanks]

Now other question: how will i retrieve the default value from the descriptor? (if is it possible?)

I’m asking this because that would allow to only change default values in only one file - no need to look trought code.

Sorry, still don’t understand how to do it, can you give a sample?

What did you try? Where did you get stuck? What did you understand so far?

I tried eventBus and succeeded. So far, I want to know what EventBus, _listteners mean, I don’t understand why it is commandStack.shape.create.postExecute, please forgive me for not good English.

commandStack.shape.create.postExecute is an event fired by the command stack. As the name suggests it uses the command pattern which encapsulates every modeling operation into a single command which can the be undone and redone. The command stack has a life-cycle when executing a command. The post-execute phase is entered and the event is fired after the actual command has been executed. The shape at this point has been created.

hi

I found the shape of the created element in your way, but I don’t know how to modify the default value of select through this element. Can you give me some advice?

Thank you