flowable/flowable-engine · error · ActivitiObjectNotFoundException

Cannot find task with id

Error message

Cannot find task with id ${taskId}

What it means

CreateAttachmentCmd verifies that, when a taskId is supplied, a task with that id exists; it throws ActivitiObjectNotFoundException with Task.class when findTaskById returns null. Attachments are bound to existing tasks, so a dangling id is rejected.

Solutions

  1. Verify the task exists (taskService.createTaskQuery().taskId(id).singleResult()) before attaching
  2. Correct the taskId value
  3. Confirm you are connected to the same database where the task lives

Example fix

// before
taskService.createAttachment("text", badTaskId, null, "name", "desc", content);
// after
if (taskService.createTaskQuery().taskId(taskId).singleResult() != null) {
    taskService.createAttachment("text", taskId, null, "name", "desc", content);
}
Defensive patterns

Strategy: validation

Validate before calling

boolean taskExists = taskId == null || taskService.createTaskQuery().taskId(taskId).count() > 0;

Try / catch

try { taskService.createAttachment(type, taskId, procInstId, name, desc, stream); } catch (ActivitiObjectNotFoundException e) { log.warn("Target task/process missing: {}", e.getMessage()); }

Prevention

When it happens

Trigger: taskService.createAttachment(attachmentType, taskId, processInstanceId, ...) with a taskId that does not exist (deleted, completed-and-cleaned, wrong db, or typo).

Common situations: Storing a task id in an external form/URL and attaching after the task was completed; using an id from another environment or schema; string concatenation typos.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of flowable/flowable-engine@d6d39ce1c6 (2026-09-11). Data as JSON: /api/errors/ddbfcec3a4041b4c. Report an issue: GitHub.

Appendix: source

Thrown at modules/flowable5-engine/src/main/java/org/activiti/engine/impl/cmd/CreateAttachmentCmd.java:117

            commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
                    ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_CREATED, 
                            attachment, processInstanceId, processInstanceId, processDefinitionId),
                    EngineConfigurationConstants.KEY_PROCESS_ENGINE_CONFIG);
            commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
                    ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_INITIALIZED, 
                            attachment, processInstanceId, processInstanceId, processDefinitionId),
                    EngineConfigurationConstants.KEY_PROCESS_ENGINE_CONFIG);
        }

        return attachment;
    }

    private void verifyParameters(CommandContext commandContext) {
        if (taskId != null) {
            TaskEntity task = commandContext.getTaskEntityManager().findTaskById(taskId);

            if (task == null) {
                throw new ActivitiObjectNotFoundException("Cannot find task with id " + taskId, Task.class);
            }

            if (task.isSuspended()) {
                throw new ActivitiException("It is not allowed to add an attachment to a suspended task");
            }
        }

        if (processInstanceId != null) {
            ExecutionEntity execution = commandContext.getExecutionEntityManager().findExecutionById(processInstanceId);

            if (execution == null) {
                throw new ActivitiObjectNotFoundException("Process instance " + processInstanceId + " doesn't exist", ProcessInstance.class);
            }

            if (execution.isSuspended()) {
                throw new ActivitiException("It is not allowed to add an attachment to a suspended process instance");
            }
        }

View on GitHub (pinned to d6d39ce1c6)