Get the task when completed

Hello
In my application i have a service that show all the active task that is not finish the workflow yet and work well.
But when a task is done and finish the workflow this task is no more accesible. And i would like to get them.
As i read on the documentation (History and Audit Event Log | docs.camunda.org) a service of history is provided by Camunda but i’m stuck cause i not able to get my task back by Camunda History

My code so far is like :

List historicProcessInstances = processEngine
.getHistoryService()
.createHistoricProcessInstanceQuery()
.finished() // I suppose is for the task that finish the workflow ?
.listPage(firstResult, size);

historicProcessInstances.forEach(historicRequest → {
RequestCreation requestCreation = (RequestCreation) processEngine.getHistoryService().createHistoricVariableInstanceQuery().executionIdIn(historicRequest.getId()).variableName(CamundaVariable.REQUEST).;
});
But this code didn’t work cause there is a ecxeption :HistoricVariableInstanceQueryImpl cannot be cast to fr.adeo.admintool.request.api.model.request.creation.RequestCreation.

Where RequestCreation is my object type i would like to get from the HistoricVariableInstanceQuery
I din’t find any way to get the ecxecution ID of the Task (or get the Task directly).

So my question : Is there a way to get the finished task in Camunda ? Or the task is vanish after end the workflow ?

Thanks

Hi @Yawata,

have a look at the HistoricTaskInstanceQuery. You can create one via the historyService as you did for the HistoricProcessInstanceQuery.

Cheers,
Miklas

Hello @Miklas, thanks for your reply
I’ve tested the HistoricTaskInstanceQuery as below :

TaskQuery taskQuery = processEngine.getTaskService().createTaskQuery();
List historicTaskInstance = processEngine.getHistoryService().createHistoricTaskInstanceQuery().processFinished().list(); // or finished().list()
historicTaskInstance.forEach(x -> {
// Get the task by executionId
taskQuery.executionId(x.getExecutionId());
});
return taskQuery.list().parallelStream().map(task -> (RequestCreation) processEngine.getTaskService().getVariable(task.getId(), CamundaVariable.REQUEST))
.filter(Objects::nonNull).collect(Collectors.toList());

In my case historicTaskInstance’s size is 217 when using processFinished (and more than 660 with finished) but the taskquery list is empty and return nothing (in both case).

When having a HistoricTaskInstance how can i get the Task object back ? (Or other object that i can extract my RequestCreation from that object)

Thanks

Hi @Yawata,

the Task Object does not exist anymore once it has finished/canceled/deleted. This is the same for other objects, like ProcessInstances, Variables, …
You can only retrieve information about those terminated instances via the history service. In the case of Tasks you would get HistoricTaskInstances. Have a look at this page in the docs.

Cheers,
Miklas

2 Likes

Try something like the below. The history service can fetch you both active and completed tasks.

HistoricTaskInstanceQuery  historicTaskInstanceQuery = historyService.createHistoricTaskInstanceQuery();
List<HistoricTaskInstance>  taskInstanceList =   historicTaskInstanceQuery.processInstanceId(processId).list();

hello, how can i retrieve information about those task that does not complete? I read the doc but not find the api for this ? Can I only parse the unfinished tasks from the xml?