cancel
Showing results for 
Search instead for 
Did you mean: 

Variables Array Always Empty in REST 'Start a Process Instance' Resource Results

ryanj1
Champ in-the-making
Champ in-the-making
First, I'm using Activiti 5.15. (If this has been fixed in V5.15.1, let me know; my understanding is that version is exclusively for the MySQL 5.6 bug.)

When calling the "Start a Process Instance" REST resource, I noticed that the "variables" array was always empty, regardless of whether a process instance had associated variables or not and regardless of whether it was in process or completed. After looking into the code, it appeared that this snippet was causing the issue:

if (processInstance.getProcessVariables() != null) {
    Map<String, Object> variableMap = processInstance.getProcessVariables();
    for (String name : variableMap.keySet()) {
      result.addVariable(createRestVariable(securedResource, name, variableMap.get(name),
          RestVariableScope.LOCAL, processInstance.getId(), VARIABLE_PROCESS, false));
    }
  }

Of specific importance is the "processInstance.getProcessVariables()" call; it was always returning a Map with size 0.

I added the following code instead of the code above, and now the resource always returns a complete variable list:

RuntimeService runtimeService = ActivitiUtil.getRuntimeService();
    HistoryService historyService = ActivitiUtil.getHistoryService();
    if(processInstance.isEnded()) {
       //Process complete. Get variable values from the history service.
       result.setCompleted(true);
       
       Map<String, Object> variableMap = new HashMap<String, Object>();
       List<HistoricDetail> historicDetailList = historyService.createHistoricDetailQuery().executionId(processInstance.getId()).list();
       for(HistoricDetail historicDetail : historicDetailList) {
          Map<String, Integer> versionMap = new HashMap<String, Integer>();
          if(historicDetail instanceof HistoricVariableUpdate) {
             HistoricVariableUpdate historicVariableUpdate = (HistoricVariableUpdate) historicDetail;
             if(versionMap.get(historicVariableUpdate.getVariableName()) == null) {
                versionMap.put(historicVariableUpdate.getVariableName(), historicVariableUpdate.getRevision());
                variableMap.put(historicVariableUpdate.getVariableName(), historicVariableUpdate.getValue());
             }
             else {
                Integer currentRevision = historicVariableUpdate.getRevision();
                Integer previousRevision = versionMap.get(historicVariableUpdate.getVariableName());
                if(currentRevision > previousRevision) {
                   versionMap.put(historicVariableUpdate.getVariableName(), currentRevision);
                   variableMap.put(historicVariableUpdate.getVariableName(), historicVariableUpdate.getValue());
                }
             }
          }
       }
       
       for (String name : variableMap.keySet()) {
          result.addVariable(createRestVariable(securedResource, name, variableMap.get(name),
            RestVariableScope.LOCAL, processInstance.getId(), VARIABLE_PROCESS, false));
        }
    }
    else {
       //Process not complete. Get runtime variables.
       result.setCompleted(false);
       Map<String, Object> variableMap = runtimeService.getVariables(processInstance.getId());
       for (String name : variableMap.keySet()) {
          result.addVariable(createRestVariable(securedResource, name, variableMap.get(name),
            RestVariableScope.LOCAL, processInstance.getId(), VARIABLE_PROCESS, false));
        }
    }

(Note that I also added a boolean member variable to "ProcessInstanceResponse" to indicate whether a process instance had been completed or not.)

Perhaps I have something in the application misconfigured, and that is what is causing processInstance.getProcessVariables() call to return an empty set of variables? If so, please let me know. If not, maybe the code above will prove helpful to someone out there.
26 REPLIES 26

frederikherema1
Star Contributor
Star Contributor
In case the process ended immediately, the variables are retrieved from the history-service. What is the history-level, you have configured?

Can you try adding a receive-task, at the end of the process (before the end event) to see if you do get the variable, set in the script-task?

ryanj1
Champ in-the-making
Champ in-the-making
The initial posts within this thread discussed code changes that would allow a user to start a process instance (POST runtime/process-instances) and retrieve all of the process variables that exist within the process as of the time the first wait state is encountered. Those changes were made and are now a part of the base product. All you would have to do in order to retrieve those process variables that exist as of the first wait state is to include "returnVariables" as true within your POST operation.

It seems that the thread has now evolved and has become a discussion about the GET runtime/process-instances operation, which was not affected by the code change discussed in the first paragraph immediately above. With that said, while GET runtime/process-instances?includeProcessVariables=true does return the variables as noted within the thread, GET runtime/process-instances/{processInstanceId}?includeProcessVariables=true does not. However, GET runtime/process-instances/{processInstanceId}/variables *does*. So, Atif EL KHACHINE and sahusunil, it seems that you'll simply want to use GET runtime/process-instances/{processInstanceId}/variables if you want to retrieve all of the variables for a particular process instance.

frederikherema1
Star Contributor
Star Contributor
Thanks for the clarification, Ryan. Indeed different topics mixed together.

atifelkhachine
Champ in-the-making
Champ in-the-making
Thank you guys for the clarifications ^^
It is working for me now. Would be greate to update the user guide and add the parameter : returnVariables. 😉

AELK

shailk
Champ in-the-making
Champ in-the-making
I have downloaded Activiti latest version recently and I still dont see the above changes in user guide. When do u think updated user guide will have all such info available?

Thanks

trademak
Star Contributor
Star Contributor
The user guide is part of the Github project, so everybody can help us keeping the user guide up to date. You can also create a JIRA issue to have it logged.

Best regards,

jainmoheet
Champ in-the-making
Champ in-the-making
I am using the sample spring boot application and by calling the rest-api i am starting the process using the postman but even after setting the json object as

{
   "processDefinitionKey":"myprocess",
    "includeProcessVariables":"true",
   "variables": [
      {
        "name":"myVar",
        "value":"This is a variable",
      }
   ]
}

i am not getting any variables in the response using the postman.

but after starting the same when i query the browser hitting below:

http://localhost:8080/runtime/process-instances?includeProcessVariables=true

i get the variables within the json.

is it something to do with the rest client r else i am doing something wrong.

another thing in my bpm process i have start->servicetask->usertask->end

please let me know what could be the issue