Fetch all tasks from History for given processInstanceId

Hi,

I’ve a process with task as multi-instance of type sequential. I started process (ex: process instanceId = 100), then task is assigned to first user of assignee list, ex: taskId=1to user1. Now I’ve approved this task, task with id=2 is assigned to user 2.

now I queried for HistoricTaskInstance using taskId=2

HistoricTaskInstanceQuery historicTaskInstanceQuery = engine.getHistoryService().createHistoricTaskInstanceQuery();
        HistoricTaskInstance historicTaskInstance = historicTaskInstanceQuery.taskId(taskId).singleResult();

I tried

if(historicTaskInstance!=null){
String processInstanceId = historicTaskInstance.getProcessInstanceId();
                       
` List<HistoricTaskInstance> historicTaskInstances = historicTaskInstanceQuery.processInstanceId(processInstanceId).list();`
}

I was expecting size of the list to be two i.e. 1 HistoricTaskInstance with Id=1 and another HistoricTaskInstance with id=2, but it is one.

when I query DB

select * from ACT_HI_TASKINST where PROC_INST_ID_ = '100'

I see 2 rows with ID_=1 and ID_=2

Am I missing something?

How can I fetch tasks from history for a give process instance Id.

Thanks,
Suhas Madap.

Hi,

I’m able to solve it.

Could you please post the solution? That would help other users in a similar situation.

Thanks,
Thorben

Hi @thorben,

Actually I was re-using historicTaskInstanceQuery inside if statement to fetch task’s for a given process instance Id. So query would be formed with AND condition i.e.

taskId=‘1’ && processInstanceId=‘100’. So result is always 1 row.

ex:
select * from ACT_HI_TASKINST where PROC_INST_ID_ = '100' AND ID_='1'

I modified to create a new HistoricTaskQueryInstance then used processInstanceId as parameter, then result is as expected.

HistoricTaskInstanceQuery historicTaskInstanceQuery = engine.getHistoryService().createHistoricTaskInstanceQuery();
        HistoricTaskInstance historicTaskInstance = historicTaskInstanceQuery.taskId(taskId).singleResult();
if(historicTaskInstance!=null){
String processInstanceId = historicTaskInstance.getProcessInstanceId();
 HistoricTaskInstanceQuery localHistoricTaskInstanceQuery = engine.getHistoryService().createHistoricTaskInstanceQuery();
` List<HistoricTaskInstance> historicTaskInstances = localHistoricTaskInstanceQuery .processInstanceId(processInstanceId).list();`
}

Thanks,
Suhas Madap.

1 Like