TaskID in Process Variables retrieved through HistoryService

Good morning,

I want to generate a PDF Report at the end of a Process execution which would be triggered from a Java Delegate.
In my BPMN Plan I have lots of UserTasks which have form fields attached from the Modeler and as well some Input Variables (also attached with the Modeler).

My first approach was to use the HistoryService and combine the methods createHistoricTaskInstanceQuery and createHistoricVariableInstanceQuery to have a connection between the task and the variables related to this task. And even though I can add the .taskIdIn(taskIds) call to the createHistoricVariableInstanceQuery function, the taskID is always null and I can’t get the connection between the process variable and the task it is originally coming from.

Am I understanding something completely wrong or is the only option to write my own History Backend as described in the docs?

Best regards and thanks in advance,
Hendrik

Hi @hoestreich,

The returned execution id of a HistoricVariableInstance represents the “scope” in which the variable is valid. If the variable has been set locally on the user task, only then is the task id not null, since the scope of the variable is the user task.

You could you try to use the HistoricDetailQuery (see 1) to get the connection, since the returned activity instance id represents the (user) task in which the variable has been set.

Does it help you?

Cheers,
Roman

Hey @roman.smirnov,

thanks for your reply. I got it working, but I’m afraid it might no be optimal in regards of performance:

First if have a look, which iterates over all tasks
for(HistoricTaskInstance instance : taskList)
In this loop, I have another loop which iterates over all variables
for(HistoricVariableInstance varInstance : variableList)
And in this loop I finally have a loop over all details
for(HistoricDetail historicDetail : historicDetails)
In which I check whether the detail activity instance id is equal to the acitivity instance id of the task.
if(historicDetail.getActivityInstanceId().equals(instance.getActivityInstanceId()))

This works in my case, cause if they are equal the variable has been changed in the task. Somehow I had an issue that I did not get the variables from the last user task which was executed right before the service task where I make my querys… As if the information does not get into the history database before the process engine executes the next task.
But I will have a look, if I can fix that or find a workaround.

Best regars,
Hendrik