Search Process Instances by Start Date

I need to fetch a list of all process instances of a certain process that started before a certain date.

I tried using the runtimeservice:
runtimeService.createProcessInstanceQuery()
.processDefinitionKey(key)
.startedBefore(startDate)
.list();
However, this only gives me the actice process instances. I need the completed instances as well. So I tried using the historyservice:
historyService.createHistoricTaskInstanceQuery()
.processDefinitionKey(key)
.list();
But I couldn’t find a way to specify a startedBefore option. Maybe use the taskCreatedBefore? But that gives me task instances, which I could then group by process instance id. This is possible, but not practical. Is there a better way?

Hey,

The reason why you are not seeing the startedBefore is because you are creating a HistoricTaskInstanceQuery and not HistoricProcessInstanceQuery.

Instead of

historyService.createHistoricTaskInstanceQuery()
.processDefinitionKey(key)
.list();

You need to do

historyService.createHistoricProcessInstanceQuery()
.processDefinitionKey(key)
.startedBefore(startDate)
.list();

Cheers,
Filip

Such a stupid mistake. Thanks for the quick spot.