Get process instance by criteria via runtime service

Hi.

I have scheduler, which must delete “expired” process instances (active more than 2 months). In this case i need to use runtimeService (delete process instance method). But this delete method have 2 parameters: ProcessInstanceId, DeleteReason. So, can i somehow select processInstanceId by criteria: if the process exists for more than two months, get processInstanceId of this process

Best Regards.

@Sland is this processes that are still active?

To help simplify your setup, have you looked at using a Non-Interrupting Event Sub Process that has a Timer of the 2 months and have that point to a Terminating End Event

@StephenOTT Yes, processes are still active.

To help simplify your setup, have you looked at using a Non-Interrupting Event Sub Process that has a Timer of the 2 months and have that point to a Terminating End Event

Thank you, but i need another solution.

@Sland take a look at the Historic Process Instance Query: https://docs.camunda.org/javadoc/camunda-bpm-platform/7.10/org/camunda/bpm/engine/history/HistoricProcessInstanceQuery.html

you have various “Start” date filters you can use.

@StephenOTT When I try to create a query to get a list of processes by start time, an exception is thrown:
historyService.createHistoricProcessInstanceQuery().active().orderByProcessInstanceStartTime().list();
Message: Invalid query: call asc() or desc() after using orderByXX(): direction is null

In docs there are one note: Order by the start time (needs to be followed by Query.asc() or Query.desc() ).

So, can you please explain how to use this Query.asc() and Query.desc()?

@StephenOTT Oh, i got it. Sorry :slight_smile:

historyService.createHistoricProcessInstanceQuery().active().orderByProcessInstanceStartTime().asc().list();

But one more question. What is the difference between unfinished and active processes?

Maybe someone will find this usefull: active instances are not suspended. An unfinished instance may be active or suspended. A finished instance has an end time and is neither unsuspend nor active.