Delete process variable from a JavaDelegate

Hi,

I have a situation that I cannot understand and maybe some of you experts can help me :slight_smile:

I created a small BPMN diagram and a test to show it. The topic is: to delete a process variable from a JavaDelegate.

camunda-test-1.bpmn (4.3 KB)

@Test
@Deployment(resources = {"bpmn/camunda-test-1.bpmn"})
public void testVariableDeletion() {

    final String PROCESS_KEY = "MY_PROCESS";
    final String VAR_NAME = "MY_VARIABLE";
    final String VAR_VALUE = "MY_VALUE";


    Mocks.register("TASK_C_DELEGATE", new LoggerDelegate(){
        @Override
        public void execute(DelegateExecution execution) {
            execution.getProcessInstance().removeVariable(VAR_NAME);

            System.out.println("execution.variables = " + execution.getVariables());
            System.out.println("execution.processInstance.variables = " + execution.getProcessInstance().getVariables());
        }
    });

    ProcessInstanceWithVariables processInstance = runtimeService()
            .createProcessInstanceByKey(PROCESS_KEY)
            .startBeforeActivity("TASK_B")
            .setVariable(VAR_NAME, VAR_VALUE)
            .executeWithVariablesInReturn();

    assertThat(processInstance).isWaitingAt("TASK_B");
    assertThat(processInstance.getVariables())
            .containsEntry(VAR_NAME,VAR_VALUE)
            .containsKey(VAR_NAME);

    complete(task());

    assertThat(processInstance)
            .hasPassed("TASK_B")
            .hasPassed("TASK_C")
            .isEnded();

    assertThat(processInstance.getVariables())
            .doesNotContainKey(VAR_NAME);
}

The System.out.println within the mock show that the variable is not there.

However, the final assert fails, because the variable is there.

Why is that? Is there something about transactions / variable scope that I am missing?

Thanks in advance

Hi,

The processInstance object reflects the state when the #executeWithVariablesInReturn call returned. Make additional API calls to retrieve the updated state, e.g. RuntimeService#getVariables to check for variables.

Cheers,
Thorben

Very clear, thank you for your answer, thorben!