Getting ProcessInstance variable in Runtime

Hi

I am struggling to retrieve the variable content which was set in the Delegate file using the following function call:

delegateExecution.setVariable("objectDTO", responseEntity.getBody());

Essentially I am making a REST call where the result of which which is JSON object is being stored in a variable called “objectDTO”, but I can retrieve this from the RestController in order to display.

The debugger shows the instance carrying the correct variable in runtime, but I am struggling to retrieve the variable and keep encountering null exeption:

ProcessInstance instance = processEngine
    .getRuntimeService()
    .createProcessInstanceByKey("bpmnOne")
    .setVariable("httpMethod", HttpMethod.GET)
    .executeWithVariablesInReturn();

processEngine.getRuntimeService().getVariables("objectDTO");

Any help would be greatly appreciated.

In what context do you call this method?

I am simply trying to get the content of the DTO variable, which is a JSON object received from the REST call and display it.

I understand - i think you’re just using the wrong method
it needs to be something like this:

     ProcessEngineServices().getRuntimeService()
            .createVariableInstanceQuery()
            .processInstanceIdIn("someProcessInstance")
             .variableName("objectDTO")
            .list();

Thank you Niall, I’m afraid, I am still experiencing the null issue here as well, even if you take out the variableName() still returns null. I can see in the debugger tool that in the history, I have a list of the objectDTOs retrieved but not getting the current process instance’s variable values. Is the runtime architecture synch or asynch?

Can you upload your model and the code you’re using.

It can be either - depending on the model, but that shouldn’t matter in this case.

Delegate File:

public void execute(DelegateExecution delegateExecution) throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    ResponseEntity<objectDTO> responseEntity;
    ResponseEntity<List<objectDTO>> responseEntityList;

    if(delegateExecution.getVariable("httpMethod").equals(HttpMethod.GET)){
        if(delegateExecution.getVariable("id") != null) {
            responseEntity = restTemplate.exchange(URL + UUID.fromString(delegateExecution.getVariable("id").toString()), HttpMethod.GET, new HttpEntity<ObjectDTO>(headers), ObjectDTO.class);
            delegateExecution.setVariable("objectDTO", responseEntity.getBody());
            delegateExecution.setVariable("retrieveStatus", true);
        } else {
            responseEntityList = restTemplate.exchange(URL, HttpMethod.GET, new HttpEntity<List<ObjectDTO>>(headers), new ParameterizedTypeReference<List<ObjectDTO>>() {});
            delegateExecution.setVariable("objectListDTO", responseEntityList.getBody());
            delegateExecution.setVariable("retrieveAllStatus", true);
        }
    } 
}

RestController:

@GetMapping("/")
public ResponseEntity<List<ObjectDTO>> getAllRecords() throws URISyntaxException {
    ProcessInstance instance = processEngine
        .getRuntimeService()
        .createProcessInstanceByKey("bpmnOne")
        .setVariable("httpMethod", HttpMethod.GET)
        .executeWithVariablesInReturn();

    List<ObjectDTO> result = (List<ObjectDTO>) processEngine.getRuntimeService().getVariables("objectListDTO");

    return ResponseEntity.ok().body(result);
}

Model:

Attached.bpmn_one.bpmn (4.3 KB)

OK - now i get it. The process is finished before you make the query for the variable so it wouldn’t be in the runtime any more.
The good new is that it’s really easy to solve - you need to just change the return type when you start the process

ProcessInstanceWithVariables instance = processEngine
        .getRuntimeService()
        .createProcessInstanceByKey("bpmnOne")
        .setVariable("httpMethod", HttpMethod.GET)
        .executeWithVariablesInReturn();

Then query the object for the variable.

instance.getVariables();

Thank you Niall, makes sense, finally managed to retrieve it.

Have a look on this library:

https://www.holunda.io/camunda-bpm-data/quick-start/

2 Likes