cancel
Showing results for 
Search instead for 
Did you mean: 

How to get next User Task for a specific user?

shukla_raghav
Champ in-the-making
Champ in-the-making
Well, here is a logical question that pops up in my mind after being familiar with Activiti for nearly 3 months. Its an important question for me since i am just about to use Activiti BPM for a professional application in couple of days and currently in design phase for the same.

When we design a BPMN process, we define the various activities to be performed by the user and system, in a specified SEQUENCE and a WELL DEFINED ORDER. We also define which USER TASK is assigned. Thereby the BPMN engine knows which User has what task Assigned and also in what order. Then why do we need to pass the Task Definition ID for fetching the user task in the following manner

              
 Task tasks = processEngine.getTaskService().createTaskQuery().taskDefinitionKey("fof").list();
      
Why is it that we need to tell activiti ID of the task when it already has the process definition that defines tasks and its candidate users. Why cant we have something like this in activiti to fetch next task for the current user
     
               
Task tasks = processEngine.getTaskService().getNextTask(User currentUser);

I know there is processEngine.getTaskService().createTaskQuery().taskAssignee(), but is returns all the tasks that are assigned to a specific user and not the next task for the user.

Please suggest what should be done.
7 REPLIES 7

jbarrez
Star Contributor
Star Contributor
I know there is processEngine.getTaskService().createTaskQuery().taskAssignee(), but is returns all the tasks that are assigned to a specific user and not the next task for the user.

I don't really understand what you mean with 'next task'.
Is it a task that doesn't exist yet because the process instance hasn't arrived yet there?
Is it a task that does exist, but is not yet claimed (ie only candidate user/group and nobody has claimed it yet) ?
Or is it something else?

shukla_raghav
Champ in-the-making
Champ in-the-making
Let me explain it with the following example as sequence of activities within a process

1. START PROCESS

2. USER TASK-A
     [taskDefinitionKey : TA, assignee  : "JOHN", description : enter EmpID and timesheet]

3. SERVICE TASK-1
     [description : Compute Salary]

4. USER TASK-B
     [taskDefinitionKey : TB, assignee : "JOHN", description : submit Medical Bills for reimbursment]

5. USER TASK-C
     [taskDefinitionKey : TC, assignee : "LISA", description : approve Medical bills ]

6. END PROCESS


Now in the above process both the User Task have been pre-assigned to user JOHN and LISA resp. but in order to access these task programmatically at any point in the code we need to write the following code to access the task

    List<Task> tasks = processEngine.getTaskService().createTaskQuery().taskDefinitionKey("TA").list();

why do we need to pass TaskDefinitionKey in order to fetch the task object. In that case the client code interacting with the activity engine MUST always know the all the taskDefinitionIDs to fetch any of the task in order to complete the task. which does not seem to be a very good idea. Rather the client code should only know about current user data like the "assigneeName". So once the process starts the current process is at TASK-A, which is assigned to JOHN and in order to access the above task for completion the client Code (may be a Spring MVC Controller or a Struts Action) does not need to know the taskDefinitionID i.e. TA, rather just passing the assignee name i.e. "JOHN" to the engine the client code should be able to retrieve the TASK-A. and similarly for the other TASK.

In case the Task has been assigned to a specific group rather than a user, still a user should be able to access the TASK by passing username in case the use is a candidate of the assigned group.

I hope this explains my doubt.

pkromolowski
Champ in-the-making
Champ in-the-making
First of all, what would the your getNextTask method be supposed to return when there would be multiple "next tasks" for the same user  (ie. multi-instance tasks or user tasks following the parallel gateway) ?
Second - how would this work for assignements defined using expressions? How do you want the engine to evaluate an expression defined on the "next" task, if this task doesn't even exist yet? Please remember that you are "preassigning" a task on a definition level. When you deploy the BPMN definition - none of the tasks aren't created and their preassignements are unknown to the engine.
Third - if you REALLY, REALLY want to implement this bad idea (in my opinion of course), I think you should handle it during BPMN parse (using BpmnParseHandler or BpmnParseListener). You could then try to follow the sequence flow after each task.

Best regards,

shukla_raghav
Champ in-the-making
Champ in-the-making
Well its not as bad idea as it seems ! will reply on the same in a couple of days

pkromolowski
Champ in-the-making
Champ in-the-making
I'm looking forward for your solution, I could have misunderstood something from your original post and this is why I called this a bad idea.

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
why do we need to pass TaskDefinitionKey in order to fetch the task object

You don't. Have a closer look at the taskquery api. You can query by assignee to

Yes you are right,but to query by assignee first we need to set the assignee. I am stuck here itself,suppose two user task with two different assignee.On starting the process i get access to only first user task because for activiti engine the 2nd user task doesn't exist still.But i want to pre-assign the assignee to all user tasks and start the process flow,querying by assignee.