{"record":{"id":"e12b94efc26af312","repo":"flowable/flowable-engine","slug":"cannot-find-the-case-to-migrate-with-id-caseinst","errorCode":null,"errorMessage":"Cannot find the case to migrate, with id${caseInstanceId}","messagePattern":"Cannot find the case to migrate, with id(.+?)","errorType":"exception","errorClass":"FlowableException","httpStatus":null,"severity":"error","filePath":"modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/migration/CaseInstanceMigrationManagerImpl.java","lineNumber":216,"sourceCode":"            if (!hasPlanItemDefinition(cmmnModel, planItemDefinitionId)) {\n                validationResult.addValidationMessage(\"Invalid mapping for remove waiting for repetition plan item definition '\" + planItemDefinitionId + \"' cannot be found in the case definition\");\n            }\n        }\n        \n        for (ChangePlanItemDefinitionWithNewTargetIdsMapping changePlanItemDefinitionIdMapping : document.getChangePlanItemDefinitionWithNewTargetIdsMappings()) {\n            if (!hasPlanItemDefinition(cmmnModel, changePlanItemDefinitionIdMapping.getNewPlanItemDefinitionId())) {\n                validationResult.addValidationMessage(\"Invalid mapping for changing the plan item definition id from '\" + changePlanItemDefinitionIdMapping.getExistingPlanItemDefinitionId() + \n                        \"' to '\" + changePlanItemDefinitionIdMapping.getNewPlanItemDefinitionId() + \"', because the target can not be found in the case definition\");\n            }\n        }\n    }\n\n    @Override\n    public void migrateCaseInstance(String caseInstanceId, CaseInstanceMigrationDocument document, CommandContext commandContext) {\n        CaseInstanceEntityManager caseInstanceEntityManager = CommandContextUtil.getCaseInstanceEntityManager(commandContext);\n        CaseInstanceEntity caseInstance = caseInstanceEntityManager.findById(caseInstanceId);\n        if (caseInstance == null) {\n            throw new FlowableException(\"Cannot find the case to migrate, with id\" + caseInstanceId);\n        }\n\n        CaseDefinition caseDefinitionToMigrateTo = resolveCaseDefinition(document, commandContext);\n        doMigrateCaseInstance(caseInstance, caseDefinitionToMigrateTo, document, commandContext);\n    }\n    \n    @Override\n    public void migrateHistoricCaseInstance(String caseInstanceId, HistoricCaseInstanceMigrationDocument document, CommandContext commandContext) {\n        HistoricCaseInstanceEntityManager historicCaseInstanceEntityManager = CommandContextUtil.getHistoricCaseInstanceEntityManager(commandContext);\n        HistoricCaseInstanceEntity caseInstance = historicCaseInstanceEntityManager.findById(caseInstanceId);\n        if (caseInstance == null) {\n            throw new FlowableException(\"Cannot find the historic case instance to migrate, with id\" + caseInstanceId);\n        }\n        \n        if (!CaseInstanceState.END_STATES.contains(caseInstance.getState())) {\n            throw new FlowableException(\"Historic case instance has not ended and can only be migrated with the regular case instance migrate method (migrateCaseInstance) for id \" + caseInstanceId);\n        }\n","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/migration/CaseInstanceMigrationManagerImpl.java#L198-L234","documentation":"migrateCaseInstance looks up the live case instance by id before migrating. If no CaseInstanceEntity exists for the given id, this FlowableException is thrown (note: the message lacks a space before the id).","triggerScenarios":"Calling CaseInstanceMigrationManager.migrateCaseInstance(caseInstanceId, document, commandContext) with an id that matches no running case instance (already ended, wrong engine/tenant, typo, or historic-only instance).","commonSituations":"Trying to migrate a completed case (must use historic migration); id taken from a different database/engine; case deleted before migration; using definition key instead of instance id by mistake.","solutions":["Query CaseService.createCaseInstanceQuery().caseInstanceId(id) first to confirm the instance exists and is running","Use the correct id (from case instance query results) and correct engine/datasource","If the case already ended, use migrateHistoricCaseInstance instead","Fix the missing-space message if you control a fork: 'with id ' + caseInstanceId"],"exampleFix":"// before\nmigrationManager.migrateCaseInstance(caseId, document, commandContext); // caseId may not exist\n// after\nif (caseService.createCaseInstanceQuery().caseInstanceId(caseId).count() > 0) {\n    migrationManager.migrateCaseInstance(caseId, document, commandContext);\n}","handlingStrategy":"validation","validationCode":"const exists = caseService.createCaseInstanceQuery().caseInstanceId(caseId).count();\nif (exists === 0) throw new Error('No running case instance with id ' + caseId);","typeGuard":"function isRunnableCase(q) { return q.singleResult() != null && !['completed','terminated','closed'].includes(q.singleResult().state); }","tryCatchPattern":"try { migrateCaseInstance(id, doc); } catch (e) { if (String(e.message).startsWith('Cannot find the case to migrate')) { throw new NotFoundError('Case instance ' + id + ' does not exist or already ended'); } throw e; }","preventionTips":["Resolve ids from case instance queries, never from user input or memory","Route finished cases to the historic migration API","Confirm you are connected to the same engine/database that owns the instance"],"tags":["cmmn","migration","entity-not-found"],"backgroundTag":"record-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"}