I have a subprocess that contains an event called “Collaboration not required”. The subprocess is of type multi instance and has an element variable of a collection. For each element of the collection, a subprocess is started.
Now I want to retrieve the element variables of those sub processes in which the event “Collaboration not required” has been passed.
To retrieve the passed events, I could use something like this:
List listOfHistoryMilestones = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstance.getId())
.activityId(ProcessConstants.MILESTONE_COLLABORATION_NOT_REQUIRED)
.finished()
.list();
To retrieve the historic element variables of the subprocesses, I could use something like this:
List list = historyService.createHistoricVariableInstanceQuery()
.variableName(ProcessConstants.VAR_NAME_MULTI_INSTANCE_ELEMENT)
.processInstanceId(processInstance.getId())
.list();
Now how do I connect those two things, i.e. how do I retrieve the variables of the subprocesses belonging to the milestones above? I tried to use the executionId of the event but that didn’t work.
You can correlate them via activity instance IDs, although this may not be straightforward. I think you have to map the element variables into the subprocess via an input parameter, such that the element is set in the scope of the subprocess. HistoricVariableInstance#getActivityInstanceId will then return the activity instance ID of the respective subprocess instance. This should then match HistoricActivityInstance#getParentActivityInstance, since the subprocess instance is the parent activity instance of the milestone event.
Can you maybe share an executable test on github where you implemented this approach? I can then give it a look and see if I can make it work. This repo can help you get started: https://github.com/camunda/camunda-engine-unittest