Search code examples
javaactivitibpmn

Get the variable map from process instance


Is there a way to get the variable map from the process instance or i have to use the execution , using execution will not help me always in getting the variable map of the process instance since one process instance might have more than one execution


Solution

  • I've faced with similar issues. You need an execution to get variable map. As you said there can be more than one execution for a given process instance. So you need to find the root execution where your variables reside. I've written some code to find root execution. I've tested it against a process that has more than one nested call activities. I haven't tried it against sub-processes but I think you can get it work:

    ExecutionEntity executionEntity = (ExecutionEntity) runtimeService.createExecutionQuery().executionId(executionId).singleResult();
    if (executionEntity == null) {
        return null;
    }
    String parentId = executionEntity.getParentId();
    boolean parentNotEmpty = StringUtils.isNotEmpty(parentId);
    String superExecutionId = executionEntity.getSuperExecutionId();
    boolean superNotEmpty = StringUtils.isNotEmpty(superExecutionId);
    if (parentNotEmpty) {
        return getRootExecution(parentId);
    } else if (superNotEmpty) {
        return getRootExecution(superExecutionId);
    } else {
        return executionEntity;
    }
    

    This snippet gets any execution id that belongs to your process instance, you can give any of them. It returns the root execution. Then using this code you can get your variable:

    Object variable = getRuntimeService().getVariable(rootExecutionId, variableName);