Search in history all the processes that had a variable

Hello,

I have a process that has two variables in the context, one is userId and the other one is comment, I want to find all the comments of the user using the history service.

I tried to get the process instances but they don’t have the variables, I was able to find the history values for the userId and for the comments but didn’t manage to find them in combination.

In case is not clear I want something similar to this query:
Select comments from historyService
where userId= input.

I didn’t manage to find something, do you have any suggestions?

You should be able to link the variable pairs by their procInstId in the history table…

I only managed to search only by one variable:

engine.getHistoryService().createHistoricVariableInstanceQuery()
.processDefinitionId(delegateExecution.getProcessDefinitionId())
.variableValueEquals(“userId”,“54354”)
.orderByVariableName().asc().list();

But I didn’t find a way to get the comments for this user

After you got your list of userIDs you should be able to get each processInstanceId of each userID-Variable ( getProcessInstanceId ())
Since there seems to be no processInstanceIdIn() you got to loop over those processInstanceIds and query for the comments variables associated with each of those Ids. ( processInstanceId(String processInstanceId).variableId(String id) )

Hope this helps.

1 Like

Hi,

I managed to do it like this:

  final List<HistoricVariableInstance> historicVariableInstances = engine.getHistoryService().createHistoricVariableInstanceQuery()
                .processDefinitionId(delegateExecution.getProcessDefinitionId())
                .variableValueEquals("userdId", "421")
                .orderByVariableName().asc().list();

Then get the processInstanceId for that user.

 List<String> processInstanceIds = historicVariableInstances.stream().map(HistoricVariableInstance::getProcessInstanceId).collect(Collectors.toList());

And after that get the historic variable for the comment

List historicVariableInstances = engine.getHistoryService().createHistoricVariableInstanceQuery().processInstanceIdIn(processInstanceIds .stream().toArray(String[]::new)).variableName(“comment”).list();

I didn’t manage to find a easier way but this works for the moment.