Reopen the task which is completed based on taskId

Hi everyone,
I want to write the code for the reopen the tasks which is marked completed.
Guide me how to do that in coding.
HistoricTaskInstance h = historyService.createHistoricTaskInstanceQuery().finished().taskId(taskId).singleResult();

You can’t reopen a task which has been completed, because the process will have proceeded to the next step(s) after the task was completed. Re-completing the task might cause all kinds of problems, especially with inconsistencies of the process state.

However, Camunda provides a way to move the process instance to another state and pick up from there. It’s called the Modification API and is applied to the runtime instances, not the history. So in your case, with a user task, it’s not a re-opening of the same task id, but a “let’s move everything over to this user task, then continue from there”, which is a new instance of the same task.

3 Likes

Hi @tiesebarrell
Thanks for the solution.
I have some more queries that how do we create the new instance of the same query bases on the same task.
can you provide the details in forms of the in code form So that I can utilize it.

For a simple process with just one branch of execution at the time of the modification, you would use something like this:

String processInstanceId = taskService().createTaskQuery().taskId("current-task-id").singleResult().getProcessInstanceId();
runtimeService.createProcessInstanceModification(processInstanceId)
  .startBeforeActivity("task-key-to-go-back-to")
  .setVariable("result-variable-key-of-task-to-go-back-to", "reset-value-to-initial-or-empty")
  .cancelAllForActivity("task-key-you-are-moving-from")
  .execute();

This assumes you have the task ID for the position the process is at the moment you want to make the change. But you can find the process instance id in all kinds of other ways, if you have other information.

You indicate the task key of the task you’re moving FROM and the one you want to move back TO. You can also reset some of the TO task’s resulting variables, if you need to.

This will create a new instance of the task with key task-key-to-go-back-to. You can query which instance of the task was created using something like: taskService.createTaskQuery().processInstanceId(processInstanceId).taskDefinitionKey("task-key-to-go-back-to").singleResult()

I’m guessing at the API’s method names because I don’t have them at hand right now, but the exact names should be similar :slight_smile:

3 Likes

Thanks for your reply.
I will try it