CDI events - can retrieve activityId but can't retrieve related Task

Hi guys,

from my listener, which is defined inside a @Singleton EJB:

public void onProcessDuringTxEvents(@Observes(during = TransactionPhase.IN_PROGRESS) final BusinessProcessEvent businessProcessEvent) {

I successfully retrieve the taskId when a “create” event occurs:
List activityIds = runtimeService.getActiveActivityIds(executionId);

The problem is that I cannot retrieve the task (nor the HistoricTaskInstance) using that activityId:

Task task = taskService.createTaskQuery().activityInstanceIdIn(activityId).singleResult();

HistoricTaskInstance = historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).activityInstanceIdIn(activityId).list();

Can somebody explain why? How could I get it? Maybe I’m not using the right TransactionPhase? It’s strange that I can retrieve the taskId thru the runtimeService but not the associated task for that Id nor thru the taskService nor the historyService…

Thanks in advance,
Alfonso.

Hello @alfmateos,

The API is a bit misleading here…

//returns a List of BPMN Activity ID which are defined in the modeler (not the generated uuid!)
List activityIds = runtimeService.getActiveActivityIds(executionId);
Task task = taskService
           .createTaskQuery()
           .taskDefinitionKeyIn(activityIds.toArray(new String[activityIds.size()])).singleResult(); 

will do the magic…

Hope that helps!
Best, McAlm