AnsweredAssumed Answered

HistoryService returns incorrect count when task and process variables are involved

Question asked by jordiang on Dec 18, 2015
I am using the TaskService and HistoryService to get the number of pending and completed tasks associated with an entity called project. The association with a project is done either by the presence of task or process variable. 

For counting the pending tasks, I  use:
        TaskQuery query = taskService.createTaskQuery();
        query.taskOwner(accountId.toString());
        query.or();
        query.taskVariableValueEquals(ProcessVariableKey.PROJECT_ID, projectId.toString());
        query.processVariableValueEquals(ProcessVariableKey.PROJECT_ID, projectId.toString());
        query.endOr();
        cnt = query.count();

This generates a query like:

  select count(distinct RES.ID_)
  from ACT_RU_TASK RES
  left outer join ACT_RU_VARIABLE A_OR0_0 on RES.ID_ = A_OR0_0.TASK_ID_
  left outer join ACT_RU_VARIABLE A_OR0_1 on RES.PROC_INST_ID_ = A_OR0_1.PROC_INST_ID_
  WHERE RES.OWNER_ = ? and
   ( ( A_OR0_0.NAME_= ? and A_OR0_0.TYPE_ = ? and A_OR0_0.TEXT_ = ? ) or
   ( A_OR0_1.TASK_ID_ is null and A_OR0_1.NAME_= ? and A_OR0_1.TYPE_ = ? and A_OR0_1.TEXT_ = ? ) )

For counting the completed tasks, I use
        HistoricTaskInstanceQuery query = historyService.createHistoricTaskInstanceQuery();
        query.taskOwner(accountId.toString());
        query.or();
        query.taskVariableValueEquals(ProcessVariableKey.PROJECT_ID, projectId.toString());
        query.processVariableValueEquals(ProcessVariableKey.PROJECT_ID, projectId.toString());
        query.endOr();
        cnt = query.count();

This generates a similar query as above, but notice the lack of the DISTINCT in the count:

   select count(RES.ID_)
   from ACT_HI_TASKINST RES
   left outer join ACT_HI_VARINST A_OR0_0 on RES.ID_ = A_OR0_0.TASK_ID_
   left outer join ACT_HI_VARINST A_OR0_1 on RES.PROC_INST_ID_ = A_OR0_1.PROC_INST_ID_
   WHERE RES.OWNER_ = ?
    and ( ( A_OR0_0.NAME_= ? and A_OR0_0.VAR_TYPE_ = ? and A_OR0_0.TEXT_ = ? )
      or ( A_OR0_1.TASK_ID_ is null and A_OR0_1.NAME_= ? and A_OR0_1.VAR_TYPE_ = ? and A_OR0_1.TEXT_ = ? ) )

So, this query is counting the number of variables, rather than the number of tasks.

Outcomes