{"record":{"id":"f214a866dff07c80","repo":"Activiti/Activiti","slug":"cannot-find-task-with-id-taskid","errorCode":null,"errorMessage":"Cannot find task with id ${taskId}","messagePattern":"Cannot find task with id (.+?)","errorType":"exception","errorClass":"ActivitiObjectNotFoundException","httpStatus":404,"severity":"error","filePath":"activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/cmd/CreateAttachmentCmd.java","lineNumber":147,"sourceCode":"                .getEventDispatcher()\n                .dispatchEvent(\n                    ActivitiEventBuilder.createEntityEvent(\n                        ActivitiEventType.ENTITY_INITIALIZED,\n                        attachment,\n                        processInstanceId,\n                        processInstanceId,\n                        processDefinitionId\n                    )\n                );\n        }\n        return attachment;\n    }\n\n    protected TaskEntity verifyTaskParameters(CommandContext commandContext) {\n        TaskEntity task = commandContext.getTaskEntityManager().findById(taskId);\n\n        if (task == null) {\n            throw new ActivitiObjectNotFoundException(\"Cannot find task with id \" + taskId, Task.class);\n        }\n\n        if (task.isSuspended()) {\n            throw new ActivitiException(\"It is not allowed to add an attachment to a suspended task\");\n        }\n\n        return task;\n    }\n\n    protected ExecutionEntity verifyExecutionParameters(CommandContext commandContext) {\n        ExecutionEntity execution = commandContext.getExecutionEntityManager().findById(processInstanceId);\n\n        if (execution == null) {\n            throw new ActivitiObjectNotFoundException(\n                \"Process instance \" + processInstanceId + \" doesn't exist\",\n                ProcessInstance.class\n            );\n        }","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/Activiti/Activiti/blob/56435b1a97deeafdc09dd40074b056c89fba5a8a/activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/cmd/CreateAttachmentCmd.java#L129-L165","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ntaskService.createAttachment(\"text\", taskId, null, \"note\", \"desc\", content);\n// after\nTask task = taskService.createTaskQuery().taskId(taskId).singleResult();\nif (task != null) {\n    taskService.createAttachment(\"text\", taskId, null, \"note\", \"desc\", content);\n}","handlingStrategy":"validation","validationCode":"Task task = taskService.createTaskQuery().taskId(taskId).singleResult();\nif (task == null) { throw new IllegalArgumentException(\"task not found: \" + taskId); }","typeGuard":"boolean taskExists(String taskId) {\n    return taskId != null && !taskId.isEmpty()\n        && taskService.createTaskQuery().taskId(taskId).singleResult() != null;\n}","tryCatchPattern":"try {\n    taskService.createAttachment(type, taskId, pid, name, desc, content);\n} catch (ActivitiObjectNotFoundException e) {\n    if (Task.class.equals(e.getResourceClass()) || e.getMessage().contains(\"Cannot find task\")) {\n        // handle missing task (log, re-fetch, or surface to user)\n    } else { throw e; }\n}","preventionTips":["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."],"tags":["activiti","task","attachment","not-found"],"backgroundTag":"record-not-found","analyzedSha":"56435b1a97deeafdc09dd40074b056c89fba5a8a","analyzedAt":"2026-09-09T21:00:06.703Z","contentChangedAt":"2026-09-09T21:00:06.703Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}