cancel
Showing results for 
Search instead for 
Did you mean: 

Forwarding variables to the next task

kpagratis
Champ in-the-making
Champ in-the-making
I have a workflow with three parallel paths. 
I want to assign task variables at any time, but have those variables be forwarded to the next task.  Is this possible?
4 REPLIES 4

frederikherema1
Star Contributor
Star Contributor
This doesn't happen out of the box. The best way I can think of is adding a TaskListener to the tasks, flushing the variables to the execution and loading them again in next task eg:

<userTask id="firstTask" name="My Task" >
  <extensionElements>
    <activiti:taskListener event="complete" class="org.activiti.FlushVarsListener" />
  </extensionElements>
</userTask>

<userTask id="nextTask" name="My Task" >
  <extensionElements>
    <activiti:taskListener event="complete" class="org.activiti.ReadVarsListener" />
  </extensionElements>
</userTask>

public class FlushListener implements TaskListener {

  public void notify(DelegateTask delegateTask) {
     for(Entry<String, Object> var : delegateTask.getVariables().entrySet()) {
       delegateTask.getExecution().setVariable(var.getKey(), var.getValue());
     }
  }
}

public class ReadVarsListener implements TaskListener {

  public void notify(DelegateTask delegateTask) {
     delegateTask.setVariableLocal("previousTaskVar", delegateTask.getExecution().getVariable("taskVar"));
     …
  }
}

tombaeyens
Champ in-the-making
Champ in-the-making
Frederik, are you sure?

If you don't declare variables and you set the variable value the first time, then the variables are created in the process instance scope.  Which means that they will be visible in the next task.  no?

In other words, I think it should just work out of the box as the default behavior.

jbarrez
Star Contributor
Star Contributor
This would indeed work, but only if the three parallel paths dont share the same variable names.
If using task-local vars, then the solution of Frederik can be used.

kpagratis
Champ in-the-making
Champ in-the-making
thanks for the reply.  I'll try it out.