LocalVariabale API not work

Hi;
I need to get only task scope variables of an specified task excluding process scope ones and therefore I use LocalVariable rest api but response is empty and returns nothing.
Can someone tell me what I am doing wrong and help me with this issue ?

I really appreciate any help here.

BR.

Hi @amahmoodi ,

could you please provide your process model? It would help us to identify possible reasons.

For now, here is the documentation on variables and variable scopes:

Kind regards
Adagatiya

Adagatiya;
Thanks for your reply.
Here is my Process model. some explanation on medel: we have defined input local variable on every task but we can not query them on rest api with LocalVariable APi.

I really appreciate your help.

Thanks.
diagram_17.bpmn (10.7 KB)

Hi @amahmoodi,

Below post might be of help to you

1 Like

Hi @amahmoodi and @hassang ,

with a simple test case I was able to reproduce your behaviour which is confirmed by the post @hassang linked. According to the answer by a camunda developer the behaviour is intended.

My log output for reference:

14:06:59.892 [main] INFO  xxx.BpmnTest - Process: []
14:06:59.893 [main] INFO  xxx.BpmnTest - Process Local: []
14:06:59.943 [main] INFO  xxx.BpmnTest - Task Execution: [Operator, firstresult, allowDelegation, allowFrequentDelegation, secondresult, assigneeType, assigneeApproach]
14:06:59.951 [main] INFO  xxx.BpmnTest - Task Execution Local: [Operator, firstresult, allowDelegation, allowFrequentDelegation, secondresult, assigneeType, assigneeApproach]
14:06:59.966 [main] INFO  xxx.BpmnTest - Task: [Operator, firstresult, allowDelegation, allowFrequentDelegation, secondresult, assigneeType, assigneeApproach]
14:06:59.971 [main] INFO  xxx.BpmnTest - Task Local: []

As workaround you should be able to use a combination of the following endpoints:

Camunda Platform REST API getTask
Camunda Platform REST API getLocalExecutionVariables
Camunda Platform REST API getTaskLocalVariables

Basically I propose retrieving the execution id of your task and then get the local variables of this child execution around the task. This would return all variables that were provided via input mapping. If you merge these with the task local variables you should have everything.

As verification I extended my test case. I used your input variables as well as four more variables called “PROCESS_VARIABLE”, “LOCAL_PROCESS_VARIABLE”, “TASK_VARIABLE” and “LOCAL_TASK_VARIABLE”.

19:58:12.978 [main] INFO xxx.BpmnTest - Process: [TASK_VARIABLE, PROCESS_VARIABLE, LOCAL_PROCESS_VARIABLE]
19:58:12.987 [main] INFO  xxx.BpmnTest - Process Local: [TASK_VARIABLE, PROCESS_VARIABLE, LOCAL_PROCESS_VARIABLE]
19:58:13.049 [main] INFO  xxx.BpmnTest - Task Execution: [Operator, firstresult, TASK_VARIABLE, allowDelegation, allowFrequentDelegation, secondresult, assigneeType, assigneeApproach, PROCESS_VARIABLE, LOCAL_PROCESS_VARIABLE]
19:58:13.062 [main] INFO  xxx.BpmnTest - Task Execution Local: [Operator, firstresult, allowDelegation, allowFrequentDelegation, secondresult, assigneeType, assigneeApproach]
19:58:13.103 [main] INFO  xxx.BpmnTest - Task: [Operator, firstresult, TASK_VARIABLE, allowDelegation, allowFrequentDelegation, secondresult, assigneeType, assigneeApproach, PROCESS_VARIABLE, LOCAL_TASK_VARIABLE, LOCAL_PROCESS_VARIABLE]
19:58:13.116 [main] INFO  xxx.BpmnTest - Task Local: [LOCAL_TASK_VARIABLE]

In case you want to have a deeper look onto this, here is the source code of my final test case:

@Slf4j
@Deployment(resources = "processes/diagram_17.bpmn")
public class BpmnTest {

    @RegisterExtension
    public static ProcessEngineExtension processEngineExtension = ProcessEngineExtension.builder()
                                                                                        .useProcessEngine(new StandaloneInMemProcessEngineConfiguration().buildProcessEngine())
                                                                                        .build();

    @Test
    public void variableScopes() {
        ProcessInstance processInstance = runtimeService().startProcessInstanceByKey(
                "MyNewProcess",
                Map.of("PROCESS_VARIABLE", "I DO NOT WANT TO SEE YOU")
        );
        BpmnAwareTests.assertThat(processInstance).isNotEnded();

        runtimeService().setVariableLocal(processInstance.getProcessInstanceId(), "LOCAL_PROCESS_VARIABLE", "I DO NOT WANT TO SEE YOU");
        taskService().setVariable(task().getId(), "TASK_VARIABLE", "I DO NOT WANT TO SEE YOU");
        taskService().setVariableLocal(task().getId(), "LOCAL_TASK_VARIABLE", "I DO NOT WANT TO SEE YOU");

        log.info("Process: {}", runtimeService().getVariables(processInstance.getProcessInstanceId()).keySet());
        log.info("Process Local: {}", runtimeService().getVariablesLocal(processInstance.getProcessInstanceId()).keySet());
        Assertions.assertThat(runtimeService().getVariables(processInstance.getProcessInstanceId()).keySet())
                  .as("Process variables only contain both global and local process variables and task variables")
                  .containsOnly("PROCESS_VARIABLE", "LOCAL_PROCESS_VARIABLE", "TASK_VARIABLE");
       Assertions.assertThat(runtimeService().getVariablesLocal(processInstance.getProcessInstanceId()).keySet())
                  .as("Process local variables only contain both global and local process variables and task variables")
                  .containsOnly("PROCESS_VARIABLE", "LOCAL_PROCESS_VARIABLE", "TASK_VARIABLE");

        log.info("Task Execution: {}", runtimeService().getVariables(task().getExecutionId()).keySet());
        log.info("Task Execution Local: {}", runtimeService().getVariablesLocal(task().getExecutionId()).keySet());
        Assertions.assertThat(runtimeService().getVariables(task().getExecutionId()).keySet())
                  .as("Task execution variables contain all variables except for local task variables")
                  .containsExactlyInAnyOrder("PROCESS_VARIABLE", "LOCAL_PROCESS_VARIABLE", "TASK_VARIABLE", "allowDelegation", "allowFrequentDelegation", "assigneeApproach", "assigneeType", "firstresult", "Operator", "secondresult");
        Assertions.assertThat(runtimeService().getVariablesLocal(task().getExecutionId()).keySet())
                  .as("Task execution local variables contain only input variables")
                  .containsExactlyInAnyOrder("allowDelegation", "allowFrequentDelegation", "assigneeApproach", "assigneeType", "firstresult", "Operator", "secondresult");

        log.info("Task: {}", taskService().getVariables(task().getId()).keySet());
        log.info("Task Local: {}", taskService().getVariablesLocal(task().getId()).keySet());
        Assertions.assertThat(taskService().getVariables(task().getId()).keySet())
                  .as("Task variables only contain both global and local process variables, both global and local task variables and input variables")
                  .containsExactlyInAnyOrder("PROCESS_VARIABLE", "LOCAL_PROCESS_VARIABLE", "TASK_VARIABLE", "LOCAL_TASK_VARIABLE", "allowDelegation", "allowFrequentDelegation", "assigneeApproach", "assigneeType", "firstresult", "Operator", "secondresult");
        Assertions.assertThat(taskService().getVariablesLocal(task().getId()).keySet())
                  .as("Task local variables only contain local task variables")
                  .containsExactlyInAnyOrder("LOCAL_TASK_VARIABLE");
    }
}

Kind regards
Adagatiya