cancel
Showing results for 
Search instead for 
Did you mean: 

How to get process variables in a taskListener?

c-shark
Champ in-the-making
Champ in-the-making
Hi!
I've got the following in my bpmn:

    <userTask id="assembleContract" name="Assemble Contract" activiti:candidateGroups="${candidateGroups}">
      <documentation>doc…</documentation>
      <extensionElements>
        <activiti:taskListener event="create" expression="${orgContractRegService.createOrganizationAndProfile(task)}"></activiti:taskListener>
      </extensionElements>
    </userTask>


And the corresponding java code:

  public void createOrganizationAndProfile(DelegateTask task) throws Exception {
    LOGGER.info("Executing createOrganizationAndProfile() listener");
    task.getVariable("organization"); //null
  }


The problem is that I can't get process variables in the listener. Interesting fact: the getVariableNames() is successful, I got back the variable names (but only the names…).
It doesn't work even when I give execution as parameter instead of task.
It works fine when I run it as a Service Task.

Can anyone help?
Thanks
4 REPLIES 4

pkonyves
Champ in-the-making
Champ in-the-making
where/how do you define the variables?

c-shark
Champ in-the-making
Champ in-the-making
I try to run this in a unit test. The process variables are set when i start the process be calling
<code>runtimeService.startProcessInstanceByKey("my_workflow", processVariables);</code>
where <code>processVariables</code> is a <code>Map<String, Object></code>.
I don't think that something wrong with the process variables, because i'm not changed that part since I try to use task listeners instead of service tasks. And it worked with service tasks.

trademak
Star Contributor
Star Contributor
You can use the following lines to get the variable:

<blockcode>
DelegateExecution execution = delegateTask.getExecution();
String organization = (String) execution.getVariable("organization");
</blockcode>

Best regards,

c-shark
Champ in-the-making
Champ in-the-making
Thanks.
I wrote that i tried with execution (as given by parameter).
I figured out that the problem wasn't in Activiti. I tried to print out a JPA Entity, that tried to fetch some other data, that is not existed at the moment. It was misleading that there was no exception which one might infer the real problem.