TaskQuery always returns 0 Tasks

In my IncidentHandler, I want to get a List of all Tasks from the ProcessInstance, but my TaskQuery always returns an empty List.

taskService.getTaskQuery().list()

At the end I want to find the Task where the Incident happend.
What do I have to do to find the correct task?

Can you get the activity id from from the IncidentContext to find the task according to this:

https://docs.camunda.org/manual/7.10/user-guide/process-engine/incidents/#implement-custom-incident-handlers
https://docs.camunda.org/javadoc/camunda-bpm-platform/7.10/org/camunda/bpm/engine/impl/incident/IncidentContext.html

A HistoricTaskInstanceQuery in combination with the properties of the IncidentContext might return more results for tasks of that process instance.

I also tried:

hitoryService.createHistoricTaskInstanceQuery().list()
and
hitoryService.createHistoricTaskInstanceQuery().processDefinitionId(context.getProcessDeginitionId()).list()
but it also returns an empty List.

Hi Niels,

are you using External Tasks or User Tasks?

Cheers,
Tassilo

This is my BPMN:

I’m using the Spring Boot Starter from Camunda

  • A REST-Endpoint starts a new ProcessInstance and returns the ProcessInstanceId
  • A second REST-Endpoint inserts MessageCorrelation to a given ProcessInstanceId
  • The “Task 3” throws an Exception

This is the Code from the IncidentHandler.handleIncident(context, message) Method:

`
public Incident handleIncident(final IncidentContext context, final String message) {

    log.info("CONTEXT          activityId = {}", context.getActivityId());
    log.info("CONTEXT         executionId = {}", context.getExecutionId());
    log.info("CONTEXT     jobDefinitionId = {}", context.getJobDefinitionId());
    log.info("CONTEXT processDefinitionId = {}", context.getProcessDefinitionId());
    log.info("CONTEXT            tenantId = {}", context.getTenantId());
    log.info("CONTEXT       configuration = {}", context.getConfiguration());
    log.info("CONTEXT           className = {}", context.getClass().getName());

    final List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery()
            .activityIdIn(context.getActivityId()).list();
    log.info("PROCESS_INSTANCES size = {}", processInstances.size());
    processInstances.forEach(processInstance -> {
        log.info("PROCESS_INSTANCE                    id = {}", processInstance.getId());
        log.info("PROCESS_INSTANCE     processInstanceId = {}", processInstance.getProcessInstanceId());
        log.info("PROCESS_INSTANCE rootProcessInstanceId = {}", processInstance.getRootProcessInstanceId());
        log.info("PROCESS_INSTANCE   processDefinitionId = {}", processInstance.getProcessDefinitionId());
        log.info("PROCESS_INSTANCE        caseInstanceId = {}", processInstance.getCaseInstanceId());
        log.info("PROCESS_INSTANCE           businessKey = {}", processInstance.getBusinessKey());
        log.info("PROCESS_INSTANCE              tenantId = {}", processInstance.getTenantId());
        log.info("PROCESS_INSTANCE           isSuspended = {}", processInstance.isSuspended());
        log.info("PROCESS_INSTANCE             className = {}", processInstance.getClass().getName());

        final ActivityInstance activityInstance = runtimeService.getActivityInstance(processInstance.getId());
        log.info("ACTIVITY_INSTANCE                       id = {}", activityInstance.getId());
        log.info("ACTIVITY_INSTANCE               activityId = {}", activityInstance.getActivityId());
        log.info("ACTIVITY_INSTANCE             activityName = {}", activityInstance.getActivityName());
        log.info("ACTIVITY_INSTANCE             activityType = {}", activityInstance.getActivityType());
        log.info("ACTIVITY_INSTANCE parentActivityInstanceId = {}", activityInstance.getParentActivityInstanceId());
        log.info("ACTIVITY_INSTANCE activityInstances = {}", Arrays.asList(activityInstance.getChildActivityInstances())
                .stream().map(ai -> ai.getId()).collect(Collectors.toList()));
    });

    final List<Execution> executions = runtimeService.createExecutionQuery().activityId(context.getActivityId()).list();
    log.info("EXECUTIONS size = {}", executions.size());
    executions.forEach(execution -> {
        log.info("---------------------------------------------------------------------");
        log.info("EXECUTION                id = {}", execution.getId());
        log.info("EXECUTION          tenantId = {}", execution.getTenantId());
        log.info("EXECUTION processInstanceId = {}", execution.getProcessInstanceId());
        log.info("EXECUTION       isSuspended = {}", execution.isSuspended());
        log.info("EXECUTION           isEnded = {}", execution.isEnded());

    });

    final List<HistoricTaskInstance> historicTaskInstances = historyService.createHistoricTaskInstanceQuery()
            .processDefinitionId(context.getProcessDefinitionId()).list();
    log.info("HISTORIC_TASK_INSTANCES size = {}", historicTaskInstances.size());
    historicTaskInstances.forEach(historicTaskInstance -> {
        log.info("---------------------------------------------------------------------");
        log.info("HISTORIC_TASK_INSTANCE          id = {}", historicTaskInstance.getId());
        log.info("HISTORIC_TASK_INSTANCE        name = {}", historicTaskInstance.getName());
        log.info("HISTORIC_TASK_INSTANCE executionId = {}", historicTaskInstance.getExecutionId());
    });

    final List<Task> tasks = taskService.createTaskQuery().processDefinitionId(context.getProcessDefinitionId()).list();
    log.info("TASKS size = {}", tasks.size());
    tasks.forEach(task -> {
        log.info("---------------------------------------------------------------------");
        log.info("TASK                  id = {}", task.getId());
        log.info("TASK                name = {}", task.getName());
        log.info("TASK         executionId = {}", task.getExecutionId());
        log.info("TASK   processInstanceId = {}", task.getProcessInstanceId());
        log.info("TASK processDefinitionId = {}", task.getProcessDefinitionId());
        log.info("TASK            tenantId = {}", task.getTenantId());
        log.info("TASK        parentTaskId = {}", task.getParentTaskId());
        log.info("TASK     caseExecutionId = {}", task.getCaseExecutionId());
        log.info("TASK      caseInstanceId = {}", task.getCaseInstanceId());
        log.info("TASK    caseDefinitionId = {}", task.getCaseDefinitionId());
        log.info("TASK           className = {}", task.getClass().getName());

        if (task instanceof Activity) {
            log.info("TASK         is activity = true");
            final Activity activity = (Activity) task;
            log.info("TASK   isForCompensation = {}", activity.isForCompensation());
        } else {
            log.info("TASK         is activity = false");
        }
    });

    log.info("---------------------------------------------------------------------");
    final Incident incident = runtimeService.createIncident("CustomType", context.getExecutionId(),
            context.getConfiguration(), message);
    log.info("INCIDENT                  id = {}", incident.getId());
    log.info("INCIDENT        incidentType = {}", incident.getIncidentType());
    log.info("INCIDENT   processInstanceId = {}", incident.getProcessInstanceId());
    log.info("INCIDENT processDefinitionId = {}", incident.getProcessDefinitionId());
    log.info("INCIDENT     jobDefinitionId = {}", incident.getJobDefinitionId());
    log.info("INCIDENT          activityId = {}", incident.getActivityId());
    log.info("INCIDENT         executionId = {}", incident.getExecutionId());
    log.info("INCIDENT     causeIncidentId = {}", incident.getCauseIncidentId());
    log.info("INCIDENT            tenantId = {}", incident.getTenantId());
    log.info("INCIDENT       configuration = {}", incident.getConfiguration());
    log.info("INCIDENT     incidentMessage = {}", incident.getIncidentMessage());
    return incident;

}
`

This is my Logging-Output:

`
CONTEXT activityId = Task_1rpk00g
CONTEXT executionId = 5e421a55-4ae4-11e9-ad03-0242dc4520e1
CONTEXT jobDefinitionId = 4c3bff5b-4ae4-11e9-ad03-0242dc4520e1
CONTEXT processDefinitionId = test_prozess:1:4c3bd846-4ae4-11e9-ad03-0242dc4520e1
CONTEXT tenantId = null
CONTEXT configuration = 6395a0f4-4ae4-11e9-ad03-0242dc4520e1
CONTEXT className = org.camunda.bpm.engine.impl.incident.IncidentContext
PROCESS_INSTANCES size = 1
PROCESS_INSTANCE id = 5e421a55-4ae4-11e9-ad03-0242dc4520e1
PROCESS_INSTANCE processInstanceId = 5e421a55-4ae4-11e9-ad03-0242dc4520e1
PROCESS_INSTANCE rootProcessInstanceId = 5e421a55-4ae4-11e9-ad03-0242dc4520e1
PROCESS_INSTANCE processDefinitionId = test_prozess:1:4c3bd846-4ae4-11e9-ad03-0242dc4520e1
PROCESS_INSTANCE caseInstanceId = null
PROCESS_INSTANCE businessKey = null
PROCESS_INSTANCE tenantId = null
PROCESS_INSTANCE isSuspended = false
PROCESS_INSTANCE className = org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
ACTIVITY_INSTANCE id = 5e421a55-4ae4-11e9-ad03-0242dc4520e1
ACTIVITY_INSTANCE activityId = test_prozess:1:4c3bd846-4ae4-11e9-ad03-0242dc4520e1
ACTIVITY_INSTANCE activityName = Test_Prozess
ACTIVITY_INSTANCE activityType = processDefinition
ACTIVITY_INSTANCE parentActivityInstanceId = null
ACTIVITY_INSTANCE activityInstances = []
EXECUTIONS size = 1

EXECUTION id = 5e421a55-4ae4-11e9-ad03-0242dc4520e1
EXECUTION tenantId = null
EXECUTION processInstanceId = 5e421a55-4ae4-11e9-ad03-0242dc4520e1
EXECUTION isSuspended = false
EXECUTION isEnded = false
HISTORIC_TASK_INSTANCES size = 0
TASKS size = 0

Incident happend!
INCIDENT id = 63a690f0-4ae4-11e9-ad03-0242dc4520e1
INCIDENT incidentType = CustomType
INCIDENT processInstanceId = 5e421a55-4ae4-11e9-ad03-0242dc4520e1
INCIDENT processDefinitionId = test_prozess:1:4c3bd846-4ae4-11e9-ad03-0242dc4520e1
INCIDENT jobDefinitionId = null
INCIDENT activityId = Task_1rpk00g
INCIDENT executionId = 5e421a55-4ae4-11e9-ad03-0242dc4520e1
INCIDENT causeIncidentId = 63a690f0-4ae4-11e9-ad03-0242dc4520e1
INCIDENT tenantId = null
INCIDENT configuration = 6395a0f4-4ae4-11e9-ad03-0242dc4520e1
INCIDENT incidentMessage = Error while evaluating expression: ${my.taskThree(execution)}. Cause: java.lang.IllegalArgumentException: Message is not allowed to be ‘exception’!
`

Hi Niels,

the TaskService API is supposed to provide User Task interactions and via the API HistoryService#createHistoricTaskInstanceQuery you can query for the history data of User Tasks. If the Token arrives “Task 4”, it enters a wait state in this User Task. By performing taskService.getTaskQuery().list() you will get a list with one result.

Maybe it is better to directly query for the respective Incident [1] in your case?

Cheers,
Tassilo

[1] https://docs.camunda.org/manual/7.10/user-guide/process-engine/incidents/

1 Like

My goal is to determine if the Incident happens in a CompensationTask or not.
This Info is delivered by the Attribute isForCompensation of the Activity-Object.
In my Opinion, to get the Activity i need to find the corresponding Task and Cast it into an Activity-Object.

My IncidentHandler should create a CustomIncident if it’s an Incident, which happens within a CompensationTask and a FailedJobIncident if it happens within a normal Task.

I tried:
runtimeService.createIncidentQuery().list()
and it also returns an empty List

Which query do I have to execute to get all Tasks and not only UserTask and ExternalTask?

Hi Niels,

if the Incident happens in “Task 3”, you cannot query for it in a previouse Tasks (like the compensation task) because the incident will happen in an succeeding task.

Cheers,
Tassilo

I want to Query it in the Incident Handler, not in a Task

Hi Niels,

what is your use case that you are trying to cover by a custom incident implementation? Maybe we could find another solution which makes use of the default behavior of the Workflow Engine?

Cheers,
Tassilo

Hi

My Usecase is that i want to trigger the Compensations if a Incident happens in a non-CompensationTask.

I have a SchedulerJob which searches for Incidents every 2 Seconds.
My Plan was to search for Incidents of a specific custom IncidentType called “failedProcessJob”
The Scheduler should trigger the compensations for the ProcessInstance of the Incident.
If an Incident happens within a CompensationTask the SchedulerJob should ignore it cause it would not be an Incident of the “failedProcessJob”-Type.

So my plan was to add a IncidentHandler which creates Incidents of a custom “failedProcessJob”-Type, if the Incident happens by an Exception within a Task of the normal Process and creates a standart Incident of the “failedJob”-Type if the Exception happens in a CompensationTask.

Cheers,
Niels