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
- Fetch the task first (taskService.createTaskQuery().taskId(taskId).singleResult()) and only call createAttachment when it is non-null.
- Verify the taskId variable actually holds the runtime task id, not the execution id or process instance id.
- Check tenant/database alignment: ensure the engine instance (and tenant) the id came from is the one being called.
- 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
- Always fetch the task by id before mutating it.
- Store runtime task ids fresh, never cache across process restarts.
- Distinguish task id from execution id and process instance id in your data model.
- Catch ActivitiObjectNotFoundException around task-scoped operations.
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
- Cannot find task with id
- Cannot find task with id
- It is not allowed to add an attachment to a suspended task
- Process instance doesn't exist
- Cannot execute operation: task is suspended
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)