Toggle menu

Removing Tasks from Users

Once a user has claimed a task it stays with them until they complete it or, if enabled in your Self Service template, they "unclaim" it.

But what if a user claims a load of tasks then gets run over by a bus? Or what if the task is no longer relevant after a certain time? It's possible to programmatically unclaim tasks or even cancel them altogether.

Cancel with a Timer

This first example cancels a task with a timer. The timer starts counting down as soon as the execution arrives at the task, not when the task has been claimed (see below for an example of that).

Cancel on Timer

A timer boundary event is attached to the user task. It has the "Cancel activity" property checked. When the timer expires/time is reached, the user task is cancelled and the execution continues along the path leading from the timer.

We don't advise looping back to the same task from a boundary event. If you do decide to, make sure the task has been cancelled before returning to it, otherwise duplicates will be created. You could continue the process on to another user task, perhaps assigned to an "escalation" user (this task could even use the same task form as the cancelled task).

Unclaim with a Script Task

Rather than cancelling a task you can use a script to remove the current user from it, allowing other eligible users to claim it.

Unclaim Script Task

The message flow from the user task is triggered when the task is claimed. This is possible because claiming a task sends a message to the process instance, in the format _taskID_claimed, where taskID is the ID of the user task.

For example, this user task has the ID "confirmtask":

User Task ID

The message event attached to it is listening for a message reference named _confirmtask_claimed.

Message Reference

Note how the message event does not cancel the task - if it did the task would be cancelled as soon as it was claimed, making it impossible to complete.

The execution then arrives at a timer, which can be set to wait for however long you would like. Once the timer has expired the execution moves on to the script task.

The script task calls directly into the workflow engine. The call is only made if the task has not already completed, ie if the task has completed and set its _completed process variable, the script won't attempt to remove the user from it (and fail with an error).

if (typeof _confirmtask_completed === "undefined") {
    execution.getEngineServices().getTaskService().unclaim(_confirmtask_claimed.taskId);
}

getEngineServices() can be used to gain access to the task service, described in https://www.activiti.org/javadocs/org/activiti/engine/TaskService.html (opens new window). unclaim requires the ID of the task to unclaim. This is not the ID set in the modeller, but the unique ID of the task during this execution, available in the process variable set when the task is claimed (see _claimed, _released, _completed in Standard Process Variables).

Once the task has been unclaimed it can be picked up by any eligible candidate, including being reclaimed by the same user.

Last modified on 4 May 2022

Share this page

Facebook icon Twitter icon email icon

Print

print icon