cancel
Showing results for 
Search instead for 
Did you mean: 

Passing Variables in Activiti Workflows

mmarinovich
Champ in-the-making
Champ in-the-making
I'm new to Alfresco and having decided to jump in at the deep end I have just created my first Activiti workflow *YAY*… and sorry if this is a dopey question, but…

I want to know how to set a process variable from Java that will be available for display and update in my workflow.


I have a number of tasks in my workflow and as various users complete tasks data is captured and displayed again in subsequent tasks.  I also have an action that automatically starts my workflow and I want to set some variables in this action class as well.

I think, from reading various stuff on the interweb, that I should be using process variables to pass data between tasks in the workflow.  What I can't see is how I can set an Activiti process variable from Java.  In jBPM there appears to be something called executionContext which can be used but I cant see an equivalent for Activiti.

Any assistance would be welcome.

I'm using Alfresco 4.0.d on 64bit Windows 2008 R2.
1 REPLY 1

mmarinovich
Champ in-the-making
Champ in-the-making
Ok, I have managed to solve this one for myself…

In order to set properties on the workflow when creating it you simply pass the properties you want to set to workflowService.startWorkflow().  Easy.

In order to get Java access to your task and process variables you can use the Activiti TaskListener / ExecutionListener classes.  You can wire these into your workflow at defined points like so:


    <userTask id="apcCaptureTask" name="Capture New Assignment" activiti:candidateGroups="GROUP_APCAdministrators" activiti:formKey="apcwf:apcCaptureTask">
      <extensionElements>
        <activiti:taskListener event="complete" class="za.edu.icg.alfrescoapc.listener.CaptureListener"></activiti:taskListener>
      </extensionElements>
    </userTask>

…and then define a listener class that extends the relevant Activiti listener (Task or Execution)…like this:



import org.activiti.engine.delegate.TaskListener;

public class CaptureListener extends TaskListener {

   @Override
   public void notify(DelegateTask task) {
      try {
            // Pass the values we get back to the workflow
            DelegateExecution execution = task.getExecution();      
            execution.setVariable("apcwf_someId", task.getVariable("apcwf_someId"));
            execution.setVariable("apcwf_someText", task.getVariable("apcwf_sometext"));
   }
   
}

In the example above the listener is invoked when the user task completes.  When the listener is invoked it will copy the task variable values back to the workflow variables / properties.  The properties on the workflow can then be accessed later in the workflow e.g. by a subsequent task. 

It seems that a good way to provide access to variables is to define your variables in the start task of your process and then extend that task for your other tasks.  If you create your model this way then the task variables appear to be populated automatically as you move through your workflow…which is nice.

More information about these listeners can be found in the Activiti documentation here: http://activiti.org/userguide/#executionListeners