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?
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
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?
Ask for and offer help to other Alfresco Process Services and Activiti Users and members of the Alfresco team.
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.