How to add extension element to a process via fluent API

Hi, i make a process with via fluent api. When i add a serviceTask, i want add a connector via extension element. I see a function to add called addExtensionElement, but expect an BpmnModelElementInstance.
How to instanciate an object from this interface? Or how i can add this connector via fluent api?

BpmnModelInstance modelInstance2 = Bpmn.createExecutableProcess(“invoice”)
.startEvent(“inicio”)
.name(“Inicio”)
.userTask(“proceso1”)
.name(“Confirmar Inicio”)
.camundaAssignee(“kermit”)
.serviceTask(“prueba-soap”)
.addExtensionElement() /// <— what i add here??
.endEvent()
.done();

Hi @ngandres,

I guess that you can’t create the extension elements fluently. Please have a look at the docs.

Does this help you?

Best regards,
Philipp

1 Like

thanks for the answer.

That help, but still i have the doubt. Exists an function in fluent api for add extensions element, i see it ( addExtensionElement() ) but is not documented so i can’t make it work. Is still not funtional?

Hi @ngandres,

you can combine the fluent builder and extension elements in the following way:

    BpmnModelInstance modelInstance = Bpmn.createExecutableProcess("process")
        .startEvent("start")
        .done();

    CamundaProperties properties = modelInstance.newInstance(CamundaProperties.class);

    CamundaProperty property = modelInstance.newInstance(CamundaProperty.class);
    property.setCamundaName("foo");
    property.setCamundaValue("bar");

    properties.getCamundaProperties().add(property);

    StartEvent startEvent = modelInstance.getModelElementById("start");
    startEvent.builder().addExtensionElement(properties);

    Bpmn.writeModelToStream(System.out, modelInstance);
2 Likes

Ok ok i made this work. I share this code, hoping that it will serve as an example for the future. This show how to set up a connector, input and ouput parameters. You must have in the pom the dependency for Spin and the other camunda dependencies

	BpmnModelInstance modelInstance = Bpmn
			.createExecutableProcess("invoice")
			.startEvent("start")
				.name("Start")
			.userTask("process1")
				.name("Confirm Start")
				.camundaAssignee("kermit")
			.serviceTask("task1")
				.name("Task 1")
				.camundaResultVariable("response")
			.serviceTask("task2")
				.name("Task 2")
				.camundaClass(ServiceTaskDelegator.class)
			.endEvent()
			.done();
	
	CamundaConnector connector = modelInstance
			.newInstance(CamundaConnector.class);
	CamundaConnectorId connectorId = modelInstance
			.newInstance(CamundaConnectorId.class);
	CamundaInputOutput inputoutput = modelInstance
			.newInstance(CamundaInputOutput.class);
	
	CamundaInputParameter url = modelInstance
			.newInstance(CamundaInputParameter.class); <-- One parameter
	CamundaInputParameter method = modelInstance
			.newInstance(CamundaInputParameter.class); <-- Another parameter
	CamundaOutputParameter response = modelInstance
			.newInstance(CamundaOutputParameter.class);
	CamundaScript script = modelInstance
			.newInstance(CamundaScript.class);
	
	url.setAttributeValue("name", "url");
	url.setTextContent("https://jsonplaceholder.typicode.com/posts/1");
	inputoutput.getCamundaInputParameters().add(url);
	
	method.setAttributeValue("name", "method");
	method.setTextContent("GET");		
	inputoutput.getCamundaInputParameters().add(method);
	
	resp.setAttributeValue("name", "resp");
	script.setCamundaScriptFormat("JavaScript");
	script.setTextContent("S(response)"); <-- the inline script
	resp.setValue(script);
	inputoutput.getCamundaOutputParameters().add(resp);
	
	connectorId.setTextContent(HttpConnector.ID);
	connector.setCamundaConnectorId(connectorId2);
	connector.setCamundaInputOutput(inputoutput);  
	
	ServiceTask serviceTask = modelInstance.getModelElementById("task1");
	serviceTask.builder().addExtensionElement(connector);
	
	Bpmn.writeModelToStream(System.out, modelInstance);
	
	repositoryService.createDeployment().addModelInstance("invoice.bpmn", modelInstance)
			.deploy();
3 Likes