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

ssun
Champ on-the-rise
Champ on-the-rise
I take back my previous post. I added includeProcessVariables=true worked, I mistakenly added includeVariables Smiley Sad
This post is about the return when create a process instance using.

ryanj1
Champ in-the-making
Champ in-the-making
I'm glad it worked! If you need further assistance, please don't hesitate to reach out, and good luck with Activiti.

atifelkhachine
Champ in-the-making
Champ in-the-making
Hi, this has not worked for me.
Is this future already included in 5.15.1 ?

Best Regards,
Atif

atifelkhachine
Champ in-the-making
Champ in-the-making
I have tried this url :
http://kermit:kermit@localhost:8080/activiti-webapp-rest/service/runtime/process-instances?includePr...

but i'am always getting an empty variables array : <variables/>
One more thing, is that i got an XML response instead of json, even with the header : Content-Type application/json.

This test was done on 2.5.17 version.

With kind regards,
A.ELK

frederikherema1
Star Contributor
Star Contributor
What version did you test it against? Are you sure the variables are set on process-instance level, not on a child-execution?

atifelkhachine
Champ in-the-making
Champ in-the-making
I have tested against 5.17.
I have a process with start -> Script Task -> End
Script Task : i do execution.setVariable("myVar","toto");

I am wondering if it's the correct way to do the setVariable.

Best regards,
AELK

sahusunil
Champ in-the-making
Champ in-the-making
I have tried and it's working fine if you list down all process instances. I do see variables as shown below. The only question I have, Can we get variable info for particular process-instance? It works fine with list but if I try with particular process-instance, variables array is coming blank.

http://localhost:8080/activiti-rest/service/runtime/process-instances?includeProcessVariables=true - Works fine

http://localhost:8080/activiti-rest/service/runtime/process-instances/8096?includeProcessVariables=t... - Gives blank variables array

  {
           "id": "8096",
           "url": "http://localhost:8080/activiti-rest/service/runtime/process-instances/8096",
           "businessKey": null,
           "suspended": false,
           "processDefinitionId": null,
           "processDefinitionUrl": "http://localhost:8080/activiti-rest/service/repository/process-definitions/null",
           "activityId": "usertask2",
           "variables":
           [
               {
                   "name": "question",
                   "type": "string",
                   "value": "Y",
                   "scope": "local"
               }
           ],
           "tenantId": ""
       }

atifelkhachine
Champ in-the-making
Champ in-the-making
I can't understand why not worked for me 😕
i did an HTTP POST on : http://kermit:kermit@localhost:8080/activiti-webapp-rest/service/runtime/process-instances?includePr...

The request body is like :
{
"processDefinitionId":"process:1:101"
}
The response is :
<code>
<ProcessInstanceResponse>
<id>2592</id>
<url>
http://localhost:8080/activiti-webapp-rest/service/runtime/process-instances/2592
</url>
<businessKey/>
<suspended>false</suspended>
<ended>true</ended>
<processDefinitionId>process:1:101</processDefinitionId>
<processDefinitionUrl>
http://localhost:8080/activiti-webapp-rest/service/repository/process-definitions/process%3A1%3A101
</processDefinitionUrl>
<activityId>endStep</activityId>
<variables/>
<tenantId/>
<completed>true</completed>
</ProcessInstanceResponse>
</code>

——————
The script task code :
<code><scriptTask id="scriptTask" name="script task" scriptFormat="groovy">
<script>execution.setVariable("myVar", "TotoTiti");</script>
</scriptTask></code>

sahusunil
Champ in-the-making
Champ in-the-making
Can you add activiti:autoStoreVariables="true" in your scriptTask and try it again?

Thanks

atifelkhachine
Champ in-the-making
Champ in-the-making
Hi, thank you for your reply.
Unfortunetly i have already tried this and got an empty variables array :
<code>
<process id="process" isExecutable="true">
<startEvent id="startStep" name="start step" activiti:initiator="kermit"/>
<sequenceFlow id="sid-201DAF9E-D12E-498C-8D7D-3EB68159D961" sourceRef="startStep" targetRef="scriptTask"/>
<scriptTask id="scriptTask" name="script task" scriptFormat="groovy" activiti:autoStoreVariables="true">
<script>execution.setVariable("myVar", "TotoTiti");</script>
</scriptTask>
<endEvent id="endStep" name="END STEP"/>
<sequenceFlow id="sid-F0548EB1-51AE-419D-9F90-52CBAC2B722B" sourceRef="scriptTask" targetRef="endStep"/>
</process>
</code>