Form handling in Java

Hello,

I am trying to use Camunda Process Engine to run BPMN models created in modeler.

I execute the models from Java directly like this:

runtime.createProcessInstanceByKey(“some-process-definition-name”)
.setVariables(processVariables)
.executeWithVariablesInReturn()

When that call returns it has either execute the whole flow or if there are user entry steps it has stopped in those.

I can get an activity ID for example from the runtime using the process instance ID.

But how can I get my hands on the form that was defined of the “User Task” so that I can check what fields I need to populate and how do I populate the task so that the process can continue? I would need to understand where to get the right objects to read the data from and where to store the human input.

I am not using web interface in this particular case but something completely different, so therefore using HTML forms/JS/etc. are not working unfortunately.

Thanks in advance!

Cheers,
Hannu

Hi @hkroger

you can use TaskService to find out which tasks are currently available for a specific process instance (https://docs.camunda.org/javadoc/camunda-bpm-platform/7.6/org/camunda/bpm/engine/TaskService.html)
When you found your specific Task, then you can find out more about it using the FormService (https://docs.camunda.org/javadoc/camunda-bpm-platform/7.6/org/camunda/bpm/engine/FormService.html)

Hope this helps.

Best
Felix

3 Likes

Thanks a lot for your fast response!

I did now that and I was able to submit the form with formService.submitTaskForm(task.getId, variables).

I see from my debug output that it continues the execution synchronously. When that returns however, the processInstance that I have is not updated with correct state. The process has finished as it should but the processInstance that I previously used to check still has still a state isEnded() -> false.

How could I update the processInstance with the latest values or should I run some other method to make sure the process state gets updated?

Cheers,
Hannu

You need to query again for the process instance via RuntimeService#createProcessInstanceQuery. An object returned via API always represents a snapshot of that entity at the time the API call returns. In that sense, it is rather like a (web) service API than an object-oriented API.

Ok, that makes perfect sense.

One question about it: I would like to get the instance with variable but this createProcessInstanceQuery is returning just of type ProcessInstance. Is there an alternative way to make the query or should I try to get the variables some other way?

ProcessInstanceWithVariables is only returned on process start and cannot be obtained in another way. Use RuntimeService#getVariables or similar APIs to obtain variables at a later point.

The problem with this approach is that after the processing is done the runtime.getVariables doesn’t return anything.

This doesn’t work either processInstance = runtime.createProcessInstanceQuery.processInstanceId(processInstanceId).singleResult().

So how can I get hold of the processing results after the processing is done?

Then use HistoryService. You can find a lot of these things by a) exploring what kind of services and methods ProcessEngine offers or b) reading the user guide.

Cheers,
Thorben

Yeah, sorry about the stupid questions. However I got it working thanks to your help :slight_smile: