{"record":{"id":"f0c3bfc276c62822","repo":"flowable/flowable-engine","slug":"cannot-find-task-with-id-taskid-f0c3bf","errorCode":null,"errorMessage":"Cannot find task with id ${taskId}","messagePattern":"Cannot find task with id (.+?)","errorType":"exception","errorClass":"ActivitiObjectNotFoundException","httpStatus":null,"severity":"error","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/cmd/AddCommentCmd.java","lineNumber":60,"sourceCode":"        this.message = message;\n    }\n\n    public AddCommentCmd(String taskId, String processInstanceId, String type, String message) {\n        this.taskId = taskId;\n        this.processInstanceId = processInstanceId;\n        this.type = type;\n        this.message = message;\n    }\n\n    @Override\n    public Comment execute(CommandContext commandContext) {\n\n        // Validate task\n        if (taskId != null) {\n            TaskEntity task = commandContext.getTaskEntityManager().findTaskById(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(getSuspendedTaskException());\n            }\n        }\n\n        if (processInstanceId != null) {\n            ExecutionEntity execution = commandContext.getExecutionEntityManager().findExecutionById(processInstanceId);\n\n            if (execution == null) {\n                throw new ActivitiObjectNotFoundException(\"execution \" + processInstanceId + \" doesn't exist\", Execution.class);\n            }\n\n            if (execution.isSuspended()) {\n                throw new ActivitiException(getSuspendedExceptionMessage());\n            }\n        }","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/cmd/AddCommentCmd.java#L42-L78","documentation":"AddCommentCmd.execute validates an optional taskId by looking it up via the task entity manager and throws ActivitiObjectNotFoundException when no task with that id exists. Comments can be attached to a task, a process instance, or neither, but a supplied task id must be valid.","triggerScenarios":"Calling TaskService.addComment(null-as-taskId variant is skipped) with a taskId string that does not match any task: stale/finished task id, typo, or id from another datasource.","commonSituations":"Commenting on a task after it was completed and its runtime data removed; copying ids between test/production databases; passing a process instance id where a task id is expected.","solutions":["Verify the task exists before commenting: taskService.createTaskQuery().taskId(id).singleResult() != null.","Use the Task.getId() from a live Task object rather than a hand-typed or stale id.","If the task may already be completed, look it up in history (HistoryService) and attach the comment there, or add the comment with only the processInstanceId.","Check that you are not swapping the argument order of taskId and processInstanceId in addComment(taskId, processInstanceId, message)."],"exampleFix":"// before\ntaskService.addComment(taskId, procId, \"reviewed\");\n// after\nif (taskService.createTaskQuery().taskId(taskId).count() > 0) {\n    taskService.addComment(taskId, procId, \"reviewed\");\n}","handlingStrategy":"validation","validationCode":"Task task = taskService.createTaskQuery().taskId(taskId).singleResult();\nif (task == null) { throw new IllegalStateException(\"Task \" + taskId + \" not found\"); }","typeGuard":null,"tryCatchPattern":"try {\n    taskService.addComment(taskId, processInstanceId, message);\n} catch (ActivitiObjectNotFoundException e) {\n    if (e.getMessage().startsWith(\"Cannot find task with id\")) {\n        logger.warn(\"Task {} gone; posting comment on process instead\", taskId);\n        taskService.addComment(null, processInstanceId, message);\n    } else { throw e; }\n}","preventionTips":["Look up the task immediately before commenting","Do not cache task ids across transactions — runtime tasks are removed on completion","Do not swap taskId and processInstanceId arguments","Use history API for finished tasks"],"tags":["task","not-found","comment","activiti"],"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-14T11:17:12.474Z"}