Is there a way to change the user task instance owner at runtime?


#1

Let me explan.
Here is the userTask.


And the owner was replaced with a varaible that mean the owner of this user task is replaced the variable value at the process instance started.

Let us assume the user task instance was created and the variable `applicantId’ is 1.
But now for some reason I need to change the user task instance owner to 2.

So how can I do that safetly? Thanks you all.


#2

Hi @himly,

you can use the TaskService for that. Here is an example to use from within a JavaDelegate.

  public void execute(DelegateExecution execution) throws Exception {
    
    TaskService taskService = execution.getProcessEngineServices().getTaskService();
    Task task = taskService.createTaskQuery().processInstanceId(execution.getProcessInstanceId()).singleResult();
    taskService.setAssignee(task.getId(), "newAssignee");
  }

This snippet assumes that the JavaDelegate instance runs in the same process instance the task to be modified has been created from. And additionally, that there is only one active task active at the moment.

Does that help?


#3

@sebwarnke Yes And Thank your reply. I was already got the answer of my question from camunda document but anyway thank you.


#4

Hi,

When we try to find the task via the Taskservice, it’s null (in our “starting servicetask”).


#5

Can you please show me the code of you try to find task instance ?


#6

I have tried to find the task with
Task task = ProcessEngines.getDefaultProcessEngine().getTaskService().createTaskQuery().processInstanceId(execution.getProcessInstanceId()).singleResult();

and with
TaskService taskService = execution.getProcessEngineServices().getTaskService();
task = taskService.createTaskQuery().processInstanceId(execution.getProcessInstanceId()).singleResult();