cancel
Showing results for 
Search instead for 
Did you mean: 

Setting task variables from Java

fshipley
Champ in-the-making
Champ in-the-making
Hi,

Can anyone tell me the "best" (recommended) way of updating task variables from Java ?

I have tried using both the jBPM TaskInstance.setVariable() and the Alfresco WorkflowService.updateTask() methods, however I am not getting the required result.

The property I need to update (my:status) is defined in my custom task model:

<types>
   <type name="my:task">
      <parent>bpm:workflowTask</parent>
      <properties>
         <property name="my:status">
            <title>My status</title>
            <type>d:text</type>
         </property>
      </properties>
   </type>
</types>

I define the property my_status in my process definition:

<task-node name="myTaskNode">
   <task name="my:task">
      <event type="task-end">
         <action name="task-end" class="my.repo.workflow.jbpm.TaskEndActionHandler" config-type="bean">
            <status>The End</status>
         </action>
      </event>
      <controller>
         <variable name="my_status"/>
      </controller>
   </task>
</task-node>

I would like to update the my_status (my:status) property from the Java class my.repo.workflow.jbpm.TaskEndActionHandler (in the example).

I have tried:
taskInstance.setVariable("my_status", "The End");
and
workflowService.updateTask(globalId, taskProps, null, null);

The taskInstance.setVariable() method seems to update the property OK, because if I do a taskInstance.getVariable() straight after, I get the correct value … however it gets set back to the old value somewhere else along the line.

Can anyone help ?

Thanks,
Frank
3 REPLIES 3

davidc
Star Contributor
Star Contributor
Hi Frank,

Both of your solutions should work.

The taskInstance.setVariable() method seems to update the property OK, because if I do a taskInstance.getVariable() straight after, I get the correct value … however it gets set back to the old value somewhere else along the line.

How are you retrieving the value later on in the process?

I'll have a go at this myself, but can't guarantee when!

fshipley
Champ in-the-making
Champ in-the-making
Hi David,

Thanks for your reply. Now I know I'm on the right track, I've done a few more tests…

It works OK when I set a variable from the task-create and task-end events. It also works from the very first task-assign event. The task-assign event also gets fired every time you save a task (from the manage task dialog). Setting a variable doesn't seem to work in those subsequent task-assign events (although I'm not 100% sure).

My requirement is in fact to update the task status automatically once the user has set a start date and saved the task. Maybe you have another solution or work-around.

Please don't waste your time trying to reproduce my problem. Any ideas off the top of your head would be appreciated.

Thanks again,
Frank

davidc
Star Contributor
Star Contributor
The task-assign event also gets fired every time you save a task (from the manage task dialog).

I think that's a bug - it should only fire task-assign, when the task is first assigned to a person, or subsequently re-assigned - not on every save.  I'll raise a JIRA issue.

My requirement is in fact to update the task status automatically once the user has set a start date and saved the task.

Unfortunately, there isn't a task-updated event to hook into - only task-end, but that's too late for you - the status is automatically set to completed, on task end, anyway.  I'll add a JIRA enhancement for task-updated.

The only thing that comes to mind is to override the task dialog bean in the web client, and add the start date/status behaviour there.

We have thought about adding explicit "start/suspend task" buttons to the task dialog which maintain dates & status for the user.