Activiti/Activiti · error · ActivitiObjectNotFoundException

Cannot find task with id

Error message

Cannot find task with id ${taskId}

What it means

CreateAttachmentCmd looks up the target task via commandContext.getTaskEntityManager().findById(taskId). If no task with that id exists it throws ActivitiObjectNotFoundException with resource type Task. This guards addAttachment-style API calls against dangling task references.

Solutions

  1. Fetch the task first (taskService.createTaskQuery().taskId(taskId).singleResult()) and only call createAttachment when it is non-null.
  2. Verify the taskId variable actually holds the runtime task id, not the execution id or process instance id.
  3. Check tenant/database alignment: ensure the engine instance (and tenant) the id came from is the one being called.
  4. If the task belongs to a completed instance, attach to history APIs or store the attachment outside the runtime task service.

Example fix

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

Strategy: validation

Validate before calling

Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if (task == null) { throw new IllegalArgumentException("task not found: " + taskId); }

Type guard

boolean taskExists(String taskId) {
    return taskId != null && !taskId.isEmpty()
        && taskService.createTaskQuery().taskId(taskId).singleResult() != null;
}

Try / catch

try {
    taskService.createAttachment(type, taskId, pid, name, desc, content);
} catch (ActivitiObjectNotFoundException e) {
    if (Task.class.equals(e.getResourceClass()) || e.getMessage().contains("Cannot find task")) {
        // handle missing task (log, re-fetch, or surface to user)
    } else { throw e; }
}

Prevention

When it happens

Trigger: Calling taskService.createAttachment(attachmentType, taskId, processInstanceId, attachmentName, attachmentDescription, content) with a taskId that does not exist — already deleted task, completed-process task purged, or a malformed/id-mismatched string.

Common situations: Client caches task ids across process instance restarts; the task was completed and history cleanup removed it; a typo or wrong variable is interpolated into taskId; calling against a different database/tenant than where the task lives.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of Activiti/Activiti@56435b1a97 (2026-09-09). Data as JSON: /api/errors/f214a866dff07c80. Report an issue: GitHub.

Appendix: source

Thrown at activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/cmd/CreateAttachmentCmd.java:147

                .getEventDispatcher()
                .dispatchEvent(
                    ActivitiEventBuilder.createEntityEvent(
                        ActivitiEventType.ENTITY_INITIALIZED,
                        attachment,
                        processInstanceId,
                        processInstanceId,
                        processDefinitionId
                    )
                );
        }
        return attachment;
    }

    protected TaskEntity verifyTaskParameters(CommandContext commandContext) {
        TaskEntity task = commandContext.getTaskEntityManager().findById(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");
        }

        return task;
    }

    protected ExecutionEntity verifyExecutionParameters(CommandContext commandContext) {
        ExecutionEntity execution = commandContext.getExecutionEntityManager().findById(processInstanceId);

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

View on GitHub (pinned to 56435b1a97)