{"record":{"id":"0b4fcaa223014894","repo":"Activiti/Activiti","slug":"variable-does-not-exist","errorCode":null,"errorMessage":"Variable does not exist","messagePattern":"Variable does not exist","errorType":"validation","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"activiti-core/activiti-api-impl/activiti-api-task-runtime-impl/src/main/java/org/activiti/runtime/api/impl/TaskRuntimeHelper.java","lineNumber":287,"sourceCode":"\n        taskVariablesValidator.handleUpdateTaskVariablePayload(updateTaskVariablePayload);\n\n        assertVariableExists(updateTaskVariablePayload);\n\n        taskService.setVariableLocal(\n            updateTaskVariablePayload.getTaskId(),\n            updateTaskVariablePayload.getName(),\n            updateTaskVariablePayload.getValue()\n        );\n    }\n\n    private void assertVariableExists(UpdateTaskVariablePayload updateTaskVariablePayload) {\n        Map<String, VariableInstance> variables = taskService.getVariableInstancesLocal(\n            updateTaskVariablePayload.getTaskId()\n        );\n\n        if (variables == null) {\n            throw new IllegalStateException(\"Variable does not exist\");\n        }\n\n        if (!variables.containsKey(updateTaskVariablePayload.getName())) {\n            throw new IllegalStateException(\"Variable does not exist\");\n        }\n    }\n\n    public void handleCompleteTaskPayload(CompleteTaskPayload completeTaskPayload) {\n        completeTaskPayload.setVariables(\n            taskVariablesValidator.handlePayloadVariables(completeTaskPayload.getVariables())\n        );\n    }\n\n    public void handleSaveTaskPayload(SaveTaskPayload saveTaskPayload) {\n        saveTaskPayload.setVariables(taskVariablesValidator.handlePayloadVariables(saveTaskPayload.getVariables()));\n    }\n}\n","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/Activiti/Activiti/blob/56435b1a97deeafdc09dd40074b056c89fba5a8a/activiti-core/activiti-api-impl/activiti-api-task-runtime-impl/src/main/java/org/activiti/runtime/api/impl/TaskRuntimeHelper.java#L269-L305","documentation":"Thrown by TaskRuntimeHelper.assertVariableExists when updating a task variable and the task's local variable map is null — i.e. the task has no local variables at all. The runtime requires the variable to already exist before update; there is no upsert. This is the 'task has zero local variables' branch of the same assertion.","triggerScenarios":"Calling TaskRuntime.updateVariable(UpdateTaskVariablePayload) for a taskId whose taskService.getVariableInstancesLocal(taskId) returns null (no local variables).","commonSituations":"Updating a variable on a fresh task that never had variables created; wrong taskId; variable was created on the process scope, not task-local scope; variable was deleted earlier.","solutions":["Create the variable first with createVariable, then update it.","Verify the taskId is correct and that variables were created task-locally.","Catch IllegalStateException and fall back to createVariable."],"exampleFix":"// before\ntaskRuntime.updateVariable(TaskPayloadBuilder.updateVariable().withTaskId(taskId).withVariable(name, value).build());\n// after\nboolean exists = taskRuntime.variables(taskId).stream().anyMatch(v -> v.getName().equals(name));\nif (exists) {\n    taskRuntime.updateVariable(TaskPayloadBuilder.updateVariable().withTaskId(taskId).withVariable(name, value).build());\n} else {\n    taskRuntime.createVariable(TaskPayloadBuilder.createVariable().withTaskId(taskId).withVariable(name, value).build());\n}","handlingStrategy":"validation","validationCode":"boolean hasVariables = !taskRuntime.variables(taskId).isEmpty();\nboolean exists = hasVariables && taskRuntime.variables(taskId).stream()\n    .anyMatch(v -> v.getName().equals(name));\nif (!exists) {\n    taskRuntime.createVariable(TaskPayloadBuilder.createVariable().withTaskId(taskId).withVariable(name, value).build());\n}","typeGuard":null,"tryCatchPattern":"try {\n    taskRuntime.updateVariable(payload);\n} catch (IllegalStateException e) {\n    if (\"Variable does not exist\".equals(e.getMessage())) {\n        taskRuntime.createVariable(createPayload);\n    } else {\n        throw e;\n    }\n}","preventionTips":["Verify the taskId points to the intended task before updating.","Remember updates are not upserts: create first.","Check whether variables live at task-local vs process scope."],"tags":["activiti","task-variables","missing-variable","illegal-state"],"backgroundTag":"resource-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"}