link newTask to process instance

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Active Member II

link newTask to process instance

Jump to solution

I'm trying to modify Activiti 5.22 source code in order to link a task, created with the method newTask from the class TaskService, to a process instance, so that when I'm calling historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstance.getId()).finished().orderByHistoricActivityInstanceEndTime().asc().list() the list contains also the task that I added during execution.

In some way I managed to do that modifying the class NewTaskCmd in this way:

public class NewTaskCmd implements Command<Task>, Serializable {

    private static final long serialVersionUID = 1L;

    protected String taskId;
    protected String processInstanceId;
    protected String processDefinitionId;
    protected String executionId;

    public NewTaskCmd(String taskId, String processInstanceId, String processDefinitionId) {
        this.taskId = taskId;
        this.processInstanceId = processInstanceId;
        this.processDefinitionId = processDefinitionId;
        this.executionId = processInstanceId;
    }

    public Task execute(CommandContext commandContext) {
        Date currentTime = commandContext.getProcessEngineConfiguration().getClock().getCurrentTime();
        TaskEntity task = TaskEntity.create(currentTime);
        task.setId(taskId);
        task.setProcessInstanceId(processInstanceId);
        task.setProcessDefinitionId(processDefinitionId);
        task.setExecutionId(processInstanceId);
        return task;
    }

}

If I set the executionId in that way the following exception is throwed calling the complete method on any of the task of the process, even the ones included in the process definition:

ERROR org.activiti.engine.impl.interceptor.CommandContext  - Error while closing command context org.activiti.engine.ActivitiException: UserTask should not be signalled before complete

Although, if I do not set the executionId the history service query will not contain the new task that I created.

What am I doing wrong? How can I avoid the signal error?

Thanks in advance to everyone that will answer.

1 Solution

Accepted Solutions
Highlighted
Active Member II

Re: link newTask to process instance

Jump to solution

I haven't tried to create a new execution for now. So i cannot answer your question.

Yeah there is an option to change the process definition dynamically. The two following links can help for this approach.

Design by Doing 1

Design by Doing 2

But i have another question. Would it be enough to create subtasks? These can be queried in the history. 

View solution in original post

10 Replies
Highlighted
Active Member II

Re: link newTask to process instance

Jump to solution

Hi Ludovico Serrani,

i don't understand why you modify the NewTaskCmd. Why don't you just cast the new created Task to TaskEntityImpl and set your properties there?

TaskEntityImpl task = (TaskEntityImpl) taskService.newTask();

Niklas

Highlighted
Active Member II

Re: link newTask to process instance

Jump to solution

Because I didn't think about doing that.

Now I did as you say so:

TaskEntity newTask = (TaskEntity) taskService.newTask("newTask");

newTask.setProcessDefinitionId(processDefinition.getId());
newTask.setProcessInstanceId(procId);
newTask.setExecutionId(procId);
newTask.setName("New Task");
newTask.setDescription("This is a new task");
taskService.saveTask(newTask);

but I always get the error:

ERROR org.activiti.engine.impl.interceptor.CommandContext  - Error while closing command context
org.activiti.engine.ActivitiException: UserTask should not be signalled before complete

Do you know how to avoid that error?

Thank you again for solving part of my problem.

Ludovico

Highlighted
Active Member II

Re: link newTask to process instance

Jump to solution

Hmm,

i have also a problem with creating a task. When i save a task with an id i have chosen i get a NPE in the SaveTaskCmd. Maybe you should try it without setting explicitly the id and activity will create a number for it.

Just use:

newTask.setTaskDefinitionKey("newTask");

This will set the activityId like you would set in the bpmn file.

Hope this helps

Niklas

Highlighted
Active Member II

Re: link newTask to process instance

Jump to solution

Thank you again for helping me. This solved the activiyId problem but I always get that error on signal when I'm trying to complete a task, when I set executionId. I get it no matter which task I call. However if I do not set it, I really don't know how to make the history service understand that the new task is related to the process instance.

Ludovico

Highlighted
Active Member II

Re: link newTask to process instance

Jump to solution

Here it is a unit test of my issue, maybe it can be helpful to solve my problem. Niklas Menke

GitHub - ajeje93/activiti-junit-newtask: JUnit for testing the creation of a newTask with linking to... 

Highlighted
Active Member II

Re: link newTask to process instance

Jump to solution

Yes, there is a problem. While the completion of the newly created task the execution is queried. But this execution does not know about the task. So the error is raised. It seems like you have to create a new execution for this task. But that could be pretty hard.

There should be another workaround. Can you describe your use case a bit?

Highlighted
Active Member II

Re: link newTask to process instance

Jump to solution

There nothing much to say, unfortunately. The user should be able to create and execute new tasks during process execution; these newly created tasks should be shown in history service when I'm querying it on a particular process instance.

This is why I'm trying to set executionId to the same value of processInstanceId.

Will creating a new execution do a link between the task and the process instance?

Could adding those tasks to the process definition dynamically change something? Is it possible?

Is there some way in which, maybe, I can modify the sources to make the execution know about the task or I can create a new method that calls a command doing such a thing?

Thank you again for your time.

Ludovico

Highlighted
Active Member II

Re: link newTask to process instance

Jump to solution

I haven't tried to create a new execution for now. So i cannot answer your question.

Yeah there is an option to change the process definition dynamically. The two following links can help for this approach.

Design by Doing 1

Design by Doing 2

But i have another question. Would it be enough to create subtasks? These can be queried in the history. 

View solution in original post

Highlighted
Active Member II

Re: link newTask to process instance

Jump to solution

Design by doing could be a solution.

If I would like to query subtasks should I do an HistoricTaskInstanceQuery on finished tasks? If so, I don't think that this is enough because I would also like to track the process instance to which they're related.

I'll let you know for desing by doing.