cancel
Showing results for 
Search instead for 
Did you mean: 

getting tasklist size as zero even though task has been assigned to specific user

dhayanidhypazha
Champ in-the-making
Champ in-the-making
Hi,

I am working on a POC to retrive the tasklist assigned to specific user - (after the task has been assigned), I am using task listeners and execution listeners to call java class user defined method, where i wrote the code to retrive the task list. I tried with task listeners event like create, assignment, complete and execution listeners event like start and end but everytime the java method prints the task size as zero only. please find below the expression used to call user defined method and java code.

#{test.task3(fileDetails,execution)}

public void task3(ArrayList<String> map1,DelegateExecution execution)throws Exception
{
   EngineServices engineServices  = execution.getEngineServices();
   TaskService taskService = engineServices.getTaskService();
   System.out.println("+++++++++++++++++++++++++");
   
   List<Task> tasks = taskService.createTaskQuery().taskAssignee("kermit")
         .list();
   System.out.println("Task Size" + tasks.size());
if(tasks.size()!=0)
{
Task myTask = tasks.get(0);
   HashMap<String, Object> errorMessage = new HashMap<String, Object>();
   errorMessage.put("list", map1);
   taskService.setVariables(myTask.getId(),errorMessage );
}
   
}
but the same code works fine when i specify at Junit test java class. It retuns the list size as 1.
please find below the code for the same.

public class ProcessTestMyProcess3 {

   private String filename = "C:\\Documents and Settings\\xx50532\\workspace\\Samples\\src\\test\\main\\resources\\diagrams\\MyProcess3.bpmn";
   //private String filename1 = "C:\\Documents and Settings\\xx50532\\workspace\\Samples\\src\\test\\main\\resources\\diagrams\\FollowUpProcess.bpmn";
   @Autowired
   @Rule
   public ActivitiRule activitiRule;

   @Autowired
   RuntimeService runtimeService;

   @Autowired
   TaskService taskService;

   @Autowired
   RepositoryService repositoryService;

   @Test
   public void startProcess() throws Exception {
      repositoryService.createDeployment().addInputStream("myProcess3.bpmn20.xml",
            new FileInputStream(filename)).deploy();
      //repositoryService.createDeployment().addInputStream("FollowupProcess.bpmn20.xml",
            //new FileInputStream(filename1)).deploy();
      List<String> variableMap_0 = new ArrayList<String>();
      variableMap_0.add(0, "Payment");
      variableMap_0.add(1, "ACH");
      Map<String, Object> variableMap = new HashMap<String, Object>();
      variableMap.put("Test", "Test1");
      variableMap.put("fileDetails", variableMap_0);
      //variableMap.put("taskService", taskService);
      System.out.println("taskService 1 "+ taskService);
      ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess3", variableMap);
      assertNotNull(processInstance.getId());
      System.out.println("id " + processInstance.getId() + " "
            + processInstance.getProcessDefinitionId());
      
         List<Task> tasks = taskService.createTaskQuery().taskAssignee("kermit")
            .list();
      System.out.println("Task Size" + tasks.size());


I not wrote any java code to create and assign the task, I used Activiti designer to create and to assign the task to specific user.
please help me out in fixing the issue.

Thanks and Regards
Dhayanidhy P.
5 REPLIES 5

frederikherema1
Star Contributor
Star Contributor
And in what environment is it not working?

dhayanidhypazha
Champ in-the-making
Champ in-the-making
Hi, Thanks a lot for your response.
I am using Spring environment, H2 database, Tomcat server. Using eclipse Juno and testing the flow from Junit. I can able to retrive the task list if i place the logic inside junit test class but if i specify the logic in some other class and try to call from task listeners (user task) events, i can't able to retrive the task list it is always returning the task size as zero.

dhayanidhypazha
Champ in-the-making
Champ in-the-making
Any one can help me on fixing this issue.

frederikherema1
Star Contributor
Star Contributor
How do you get the TaskService instance in your task-listener? You should use the delegateTask.getEngineServices().getTaskService() to force the task-query to use the connection/transaction that's currently active in order to see they tasks that are created by the currently running process. It's possible that the task for which the listener is being called (in case of create-listener) is not yet persisted in the database, so if you expect to get that task back, you're out of luck.

dhayanidhypazha
Champ in-the-making
Champ in-the-making
It is working now, thx a lot Freder.