{"record":{"id":"9cde2bbcc1c67aa8","repo":"flowable/flowable-engine","slug":"cannot-find-task-with-id-9cde2b","errorCode":null,"errorMessage":"Cannot find task with id ","messagePattern":"Cannot find task with id ","errorType":"exception","errorClass":"FlowableObjectNotFoundException","httpStatus":null,"severity":"error","filePath":"modules/flowable-engine/src/main/java/org/flowable/engine/impl/cmd/CreateAttachmentCmd.java","lineNumber":143,"sourceCode":"                    processDefinitionId = process.getProcessDefinitionId();\n                }\n            }\n\n            eventDispatcher.dispatchEvent(FlowableEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_CREATED, attachment, \n                    processInstanceId, processInstanceId, processDefinitionId), processEngineConfiguration.getEngineCfgKey());\n            eventDispatcher.dispatchEvent(FlowableEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_INITIALIZED, attachment, \n                    processInstanceId, processInstanceId, processDefinitionId), processEngineConfiguration.getEngineCfgKey());\n        }\n\n        return attachment;\n    }\n\n    protected TaskEntity verifyTaskParameters(CommandContext commandContext) {\n        ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);\n        TaskEntity task = processEngineConfiguration.getTaskServiceConfiguration().getTaskService().getTask(taskId);\n\n        if (task == null) {\n            throw new FlowableObjectNotFoundException(\"Cannot find task with id \" + taskId, Task.class);\n        }\n\n        if (task.isSuspended()) {\n            throw new FlowableException(\"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 = CommandContextUtil.getExecutionEntityManager(commandContext).findById(processInstanceId);\n\n        if (execution == null) {\n            throw new FlowableObjectNotFoundException(\"Process instance \" + processInstanceId + \" doesn't exist\", ProcessInstance.class);\n        }\n\n        if (execution.isSuspended()) {\n            throw new FlowableException(\"It is not allowed to add an attachment to a suspended \" + execution);","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-engine/src/main/java/org/flowable/engine/impl/cmd/CreateAttachmentCmd.java#L125-L161","documentation":"CreateAttachmentCmd.verifyTaskParameters looks up the task by taskId via the task service; when no task exists with that id it throws FlowableObjectNotFoundException with the task class. This guards referential integrity of attachments: they must attach to a real task.","triggerScenarios":"Calling TaskService.createAttachment(...) with a taskId that does not match any task in the database (typo, already-deleted task, attachment created after the task/completed-and-removed task, wrong data source).","commonSituations":"Stale task ids held by a client after the task was completed and historic data cleaned; copy-pasted ids between test and prod databases; creating attachments in an async job after the task was deleted.","solutions":["Fetch the task first (taskService.createTaskQuery().taskId(taskId).singleResult()) and only create the attachment if it is non-null.","Correct the taskId source; ensure the id passed is the persistent task id (String) not a business key.","Catch FlowableObjectNotFoundException and handle as a business 'task no longer exists' case if the task may legitimately disappear."],"exampleFix":"// before\ntaskService.createAttachment(null, taskId, processInstanceId, name, description, url);\n// after\nTask task = taskService.createTaskQuery().taskId(taskId).singleResult();\nif (task == null) {\n    throw new IllegalArgumentException(\"Unknown taskId: \" + taskId);\n}\ntaskService.createAttachment(null, taskId, task.getProcessInstanceId(), name, description, url);","handlingStrategy":"validation","validationCode":"Task task = taskService.createTaskQuery().taskId(taskId).singleResult();\nif (task == null) {\n    throw new IllegalArgumentException(\"Task does not exist: \" + taskId);\n}","typeGuard":null,"tryCatchPattern":"try {\n    taskService.createAttachment(null, taskId, pid, name, desc, url);\n} catch (FlowableObjectNotFoundException e) {\n    log.warn(\"Task vanished before attachment: {}\", taskId);\n}","preventionTips":["Resolve and use Task objects returned by queries instead of cached raw ids","Handle 'task completed/deleted' races in async workflows before writing attachments","Keep attachment creation in the same transaction/session as the task interaction"],"tags":["flowable","task","attachment"],"backgroundTag":"entity-not-found","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-18T11:17:12.947Z"}