AnsweredAssumed Answered

Assign a user to a task from a tasklistener expression

Question asked by adrianbj on Apr 11, 2012
Latest reply on Apr 11, 2012 by adrianbj
Greetings to the community, I have a task that is assigned to a group, but I want when creating the task is called a service charge to verify the user with fewer tasks to be assigned to this new task. For them I use a tasklistener expression and call my service from there, the problem I have is that if you invoke the create event, acitiviti throws an exception indicating that the task with ID xxxx does not exist. and if you invoke the assigment event will not run until someone claims the work as their own.

The code

public class TaskGroupAllocatorImpl implements TaskGroupAllocator{
   private TaskService taskService;   
   private EntityManager em;   
   public void setAssigned(String taskId, Integer groupFk) throws Throwable {
      String usuarioFinal = null;
      Long noTareas = 0l;
      String queryUsers = "select e.systemuserfk from systemusergroup e where e.systemgroupfk = " + groupFk;      
      SystemGroup sg = em.find(SystemGroup.class, groupFk);      
      Query qU = em.createNativeQuery(queryUsers);      
      List<Object> usuarios = qU.getResultList();            
      usuarioFinal = "" + usuarios.get(0);
      for (Object systemUserFk : usuarios) {
         Long tmpNoTareas = 0l;
         TaskQuery tq = taskService.createTaskQuery().taskAssignee(systemUserFk.toString());
         if(tq != null){
            System.out.println("User: " + systemUserFk + " has " + tq.count() + " tasks.");
            tmpNoTareas += tq.count();
         if(tmpNoTareas < noTareas){
            usuarioFinal = systemUserFk.toString();
            noTareas = tmpNoTareas;
      taskService.setAssignee(taskId, usuarioFinal);

And I call this service from the task listener expression: