Get all Tasks a given user was/is involved

Hey there,

i can’t seem to figure it out. I am writing a custom query for the REST-API. I want to have ALL Tasks, a given user was involved like the standard REST-API does (REST/history/task?taskInvolvedUser=userId).

This is my code:
List<HistoricTaskInstance> historyTasks = historyService.createHistoricTaskInstanceQuery().taskInvolvedUser(userId).list();

Thanks in advance

Pascal

Hi Pascal,

I didn’t get what is the problem.
This query doesn’t retrieve the expected result for you or something else?

Best regards,
Yana

Hello Yana,

yes it does not give back the expected result. Here’s a lil bit more for clarification. Sry the original post was bad…
user1 and user2 had been added in another test.

RuntimeService runtimeService = processEngineRule.getRuntimeService();
IdentityService identityService = processEngineRule.getIdentityService();
TaskService taskService = processEngineRule.getTaskService();
HistoryService historyService = processEngineRule.getHistoryService();

User user1 = identityService.createUserQuery().userId("user1").singleResult();
User user2 = identityService.createUserQuery().userId("user2").singleResult();

Map<String, Object> variables = new HashMap<>();
variables.put("initiator", "user2");

ProcessInstance instance0 = runtimeService.startProcessInstanceByKey("testid", variables);
ProcessInstance instance1 = runtimeService.startProcessInstanceByKey("testid", variables);
ProcessInstance instance2 = runtimeService.startProcessInstanceByKey("testid", variables);

variables.put("initiator", "user1");
ProcessInstance instanceInitiator = runtimeService.startProcessInstanceByKey("testid", variables);

String taskId0 = taskService.createTaskQuery().processInstanceId(instance0.getProcessInstanceId()).singleResult().getId();
taskService.claim(taskId0, "user1");

NavbarDTO navbarDTO = customRestService().navbarData("user1");
assertThat(navbarDTO).hasFieldOrPropertyWithValue("userFullname", "firstNameUser1 lastNameUser1");
assertThat(navbarDTO).hasFieldOrPropertyWithValue("myTasksCount", "3");
assertThat(navbarDTO).hasFieldOrPropertyWithValue("myProcessesCount", "4");

This is my Test. The username is correct, but myTasksCount and myProcessesCount is wrong. Heres the code inside navbarData function that should retrieve this data:

List<Task> tasks = taskService.createTaskQuery().or().taskAssignee(userId).taskCandidateGroupIn(group).endOr()
        .processVariableValueNotEquals("initiator", userId).list();
navbarDTO.setMyTasksCount(tasks.size());

List<HistoricTaskInstance> historyTasks = historyService.createHistoricTaskInstanceQuery().taskInvolvedUser(userId).list();
navbarDTO.setMyProcessesCount(historyTasks.size());

Again, thanks in advance!

ok i made the taskCount work. My mistake :smiley:
Does this line only retrieves Tasks that are allready finished?

List<HistoricTaskInstance> historyTasks = historyService.createHistoricTaskInstanceQuery().taskInvolvedUser(userId).list();
navbarDTO.setMyProcessesCount(historyTasks.size());

Hi Pascal,

createHistoricTaskInstanceQuery should return all tasks not only the finished ones.

Best regards,
Yana

Am i doing something wrong in starting the processes?

i mean the processInstances are there, otherwise
List<Task> tasks = taskService.createTaskQuery().or().taskAssignee(userId).taskCandidateGroupIn(group).endOr() .processVariableValueNotEquals("initiator", userId).list(); navbarDTO.setMyTasksCount(tasks.size());

would also give me a false result, but here i get the expected 3 tasks when running the query. does the historicTaskInstanceQuery look for some variable that i did not set and that will not be set by using the
runtimeService.startProcessInstanceByKey("testid", variables);
command?