05-22-2017 09:13 AM
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(ExecutionEntity.java:1391)
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?
05-22-2017 12:54 PM
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,
Greg
05-22-2017 08:32 PM
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?
Explore our Alfresco products with the links below. Use labels to filter content by product module.