{"record":{"id":"39b31fa487a0c304","repo":"elsa-workflows/elsa-core","slug":"variable-variableid-not-found","errorCode":null,"errorMessage":"Variable '{variableId}' not found.","messagePattern":"Variable '(.+?)' not found\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Workflows.Core/Activities/SetVariable.cs","lineNumber":108,"sourceCode":"    /// </summary>\n    [Input(Description = \"The variable to assign the value to.\")]\n    public Variable? Variable { get; set; }\n\n    /// <summary>\n    /// The value to assign.\n    /// </summary>\n    [Input(Description = \"The value to assign.\")]\n    public Input<object?> Value { get; set; } = new(default(object));\n\n    /// <inheritdoc />\n    protected override void Execute(ActivityExecutionContext context)\n    {\n        // Always refer to the variable by ID to ensure that the variable is resolved from the correct scope.\n        var variableId = Variable?.Id; \n        var variable = context.ExpressionExecutionContext.EnumerateVariablesInScope().FirstOrDefault(x => x.Id == variableId);\n     \n        if (variable == null)\n            throw new InvalidOperationException($\"Variable '{variableId}' not found.\");\n        \n        var value = context.Get(Value);\n        variable.Set(context, value);\n    }\n}","sourceCodeStart":90,"sourceCodeEnd":113,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Workflows.Core/Activities/SetVariable.cs#L90-L113","documentation":"SetVariable.Execute throws InvalidOperationException when the configured Variable (matched by ID) cannot be found among the variables enumerable in the current scope of the ExpressionExecutionContext. SetVariable resolves variables strictly by ID from the enclosing scope chain; if the variable instance is not in scope, execution fails.","triggerScenarios":"Executing a SetVariable activity whose Variable reference points to a variable declared in a scope that is not an ancestor of the activity — e.g. a variable local to a different composite or loop scope, or a variable whose ID changed.","commonSituations":"Copying a SetVariable activity between workflows or scopes so its Variable ID no longer exists; deleting/renaming a variable in the designer without updating the SetVariable reference; deserialized definitions with stale variable IDs.","solutions":["Re-select the Variable on the SetVariable activity so it points to a variable in an enclosing scope.","Verify the variable is declared on the workflow root or a container that is an ancestor of the SetVariable activity.","Re-save/rebuild the workflow definition if variable IDs changed after edits or import.","Declare the variable at the workflow definition level if it must be reachable from multiple branches."],"exampleFix":"// before\n// SetVariable.Variable references variable \"MyVar\" (id=xyz) living in a sibling composite scope -> throws\n// after\n// Declare MyVar on the workflow root (or ancestor container) so EnumerateVariablesInScope() includes it,\n// then re-link SetVariable.Variable to the root-scoped variable.","handlingStrategy":"validation","validationCode":"// verify the SetVariable target is in scope before running\nvar variableId = setVariable.Variable?.Id;\nvar inScope = context.ExpressionExecutionContext.EnumerateVariablesInScope().Any(v => v.Id == variableId);\nif (!inScope)\n    throw new InvalidOperationException($\"Variable '{variableId}' is not in scope for this SetVariable activity.\");","typeGuard":"static bool IsVariableInScope(ActivityExecutionContext context, Variable? variable) =>\n    variable != null && context.ExpressionExecutionContext.EnumerateVariablesInScope().Any(v => v.Id == variable.Id);","tryCatchPattern":"try { await workflowRunner.RunAsync(workflow); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not found\") && ex.Message.Contains(\"Variable\"))\n{\n    logger.LogError(ex, \"SetVariable references an out-of-scope variable\");\n}","preventionTips":["Declare variables on the workflow root or an ancestor container of the activity.","Re-link SetVariable activities after moving them between composites or workflows.","Re-save definitions after variable edits so IDs stay consistent.","Avoid copying SetVariable activities across workflow boundaries without re-selecting the variable."],"tags":["workflow","variable","scope","state"],"backgroundTag":"resource-not-found","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}