Create form field programmatically on usertask


#1

Hey all,

I am currently trying to programmatically add a form field to a usertask. I discovered that I should use the ‘modeling’ object.

However I cannot figure out how to add a form with a single field to a usertask.

I think i should use the moddle.create object somehow. But I haven’t been able to discover how. Something like this:

var form = moddle.create('bpmn:customExtension', { 'field': {'label': 'Ask question?', 'type': 'boolean', 'defaultValue': false}});

modeling.updateProperties(element, {
		customExtension: form
	});

Can someone point me in the right direction?


#2

Found this old forum post: Add extension elements with any type (i.e. camunda:inputOutput)

This is what I was looking for.


#3

Solved with the following function:

window.updateFormField = function(elementID) {
	var element = elementRegistry.get(elementID);
	
	var extensionElements =	element.businessObject.get('extensionElements');
	
	if (!extensionElements) {
		extensionElements = moddle.create('bpmn:ExtensionElements');
	}
	
	var form = extensionElements.get('values').filter(function(elem) {
		return elem.$type == 'camunda:FormData'}
	)[0];
	
	if (!form) {
		form = moddle.create('camunda:FormData');
	}
		
	var formField = moddle.create('camunda:FormField', {'defaultValue': 'false', 'id': 'FormField_Test', 'label': 'Confirm?', 'type': 'boolean'});
	var existingFieldsWithID = form.get('fields').filter(function(elem) {
		return elem.id == formField.id;
	});
	
	for (var i = 0; i < existingFieldsWithID.length; i++) {
		form.get('fields').splice(form.get('fields').indexOf(existingFieldsWithID[i]));
	}
	form.get('fields').push(formField);
	
	extensionElements.get('values').push(form);
	modeling.updateProperties(element, {extensionElements: extensionElements});
}

Properties-panel-extension descriptor to add child element
Properties-panel-extension descriptor to add child element