4

I'm using Camunda Java Api, and i would like to change a process instance variable for a running process, is it possible ?

3
  • Yes. If you set a variable in a java delegate, it overwrites the previous value(if any). Commented Apr 3, 2019 at 7:45
  • but java delegate must be called from a task service. my question is about updating existing variable for all process instance without updating process definition. Commented Apr 3, 2019 at 8:28
  • 1
    There's a rest API for variable modification: docs.camunda.org/manual/7.6/reference/rest/process-instance/… So you can check the sources of ProcessInstanceResourceImpl.modifyProcessInstance to see what's going on... basically runtimeService.createProcessInstanceModification(processInstanceId) and then some more code. Commented Apr 3, 2019 at 10:29

2 Answers 2

5

The RuntimeService has a method ‘setVariable’ which can be called with processInstanceId, variableName and value.

You can finde the processInstance by using ‘runtimeService.createProcessInstanceQuery()....’, for example by using the process business key.

Sign up to request clarification or add additional context in comments.

Comments

4

I finally, find out how to update a variable for all running process instance :

List<ProcessInstance> processInstances =
            runtimeService.createProcessInstanceQuery()
                    .processDefinitionKey(processKey)
                    .active()
                    .list();
    processInstances.forEach(processInstance -> {
        List<Execution> executions = runtimeService.createExecutionQuery()
                .processInstanceId(processInstance.getId())
                .list();
        executions.forEach(execution -> {
            runtimeService.setVariable(execution.getId(), variableName, variableValue);
        });
    });

2 Comments

How do you execute this code ? Do you expose it as a REST endpoint ?
@Voyager3 from java delegate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.