Historic Activity Instance: When is ActivityInstanceId issued to the historic activity instance?

I am looking at a scenario where i am running unit tests and doing a query such as:

processEngine.getHistoryService()
    .createHistoricActivityInstanceQuery()
    .list()

HistoricActivityInstanceEntity[activityId=Task_1237h9u, activityName=Do 1, activityType=scriptTask, activityInstanceId=null, activityInstanceState=4, parentActivityInstanceId=7, calledProcessInstanceId=null, calledCaseInstanceId=null, taskId=null, taskAssignee=null, durationInMillis=76 ....

I have run this query after the transaction (as far as i know / can tell).

What is the logic behind the lack of activityInstanceId being issued?

@thorben any ideas?

Okay after some more testing, the part that “throws” you off is you can filter by processInstanceId, but you cannot do a generic Get on process instance Id. Meaning you can say give me all Historic Activity instances for a specific process instance, but their Activity Instance ID will be null. But if you say give me all activity instances, the process instance is null…

The work around for this was to do generic createHistoricDetailQuery first to get the specific activity instances, and then use the activity instance to use as a filter in createHistoricActivityInstanceQuery

The reason for doing this / needing this is createHistoricActivityInstanceQuery is what returns ActivityIDs, which can be correlated back to a BPMN model (such as placing overlays).

Would be very nice if there was a way to asking createHistoricDetailQuery to include the ActivityInstanceIDs in the output rather than only being available as a filter.
(it is also unclear in the docs and javadoc that it functions this way, as most other Interfaces in the API provide filters for the fields that are shown.

@thorben this is expected? any reasoning for this? and is this pattern used else where in the API, something that we can be aware of going forward?

Here is a code snippet to show the usage:

Collection<HistoricDetail> variableHistory = processEngine.getHistoryService().createHistoricDetailQuery()
                                                                                .processInstanceId(processInstanceId)
                                                                                .disableBinaryFetching()
                                                                                .variableUpdates()
                                                                                .list()

List<Map<String, Object>> activityVariableMappings = variableHistory.collect { historyItem ->
            [('activityId'): processEngine.getHistoryService().createHistoricActivityInstanceQuery()
                                            .processInstanceId(processInstanceId)
                                            .activityInstanceId(historyItem.getActivityInstanceId())
                                            .singleResult().getActivityId(),
             ('variableInstance') : historyItem.toString()
            ]
        }

Anyone from camunda can shine some light on this?

I have come across this issue again.