Get active process Instances with associated tasks

Showing results for 
Search instead for 
Did you mean: 
Member II

Get active process Instances with associated tasks

I have a requirement to get the list of all active instances along with the tasks under every instance. I did the below but unable to retrieve the tasks under the instance. Appreciate any pointers.

//Get the list of all active process instances

List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().active().list();

//Iterate through the instances and cast it to ExecutionEntity to get more info.

for(ProcessInstance processInstance : data) {
    ExecutionEntity entity = (ExecutionEntity)processInstance;
    for(TaskEntity task : entity.getTasks()) {

While performing entity.getTasks(), it throws a NPE.

java.lang.NullPointerException: null
at org.activiti.engine.impl.persistence.entity.ExecutionEntity.ensureTasksInitialized(

Looks like Context.getCommandContext() inside the ensureTasksInitialized method is returning null.

How do I get around this problem? Isn't the correct way of retrieving the tasks under an instance?

2 Replies

Re: Get active process Instances with associated tasks

Your execution entity is just a shell and will not be populated with all of the properties you need.

It will include the processInstanceId which will be enough for you to issue a task query based on the ProcessInstanceId:

List<task> myTasks = taskService.createTaskQuery.processInstanceId(entity.getId()).list();

You will then have a list of Task entities.

Just be aware, each of these queries ultimately results in a SQL call to the database so it could be potentially costly in high volume. There may be a better way of getting the same data if the instances share a common business key or process variables that you can do a direct Task query on and group by process Instance. 

Hope this helps,

Member II

Re: Get active process Instances with associated tasks

Thanks Greg. I see that there is method processInstanceIdIn(List<String> processInstanceIds) which takes in list of processInstance ids. Is it possible to use the mentioned method to get the list of tasks across many instances and group them by processInstance?