elsa-workflows/elsa-core · error · InvalidOperationException

Variable ' ' not found.

Error message

Variable '{variableId}' not found.

What it means

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.

Solutions

  1. Re-select the Variable on the SetVariable activity so it points to a variable in an enclosing scope.
  2. Verify the variable is declared on the workflow root or a container that is an ancestor of the SetVariable activity.
  3. Re-save/rebuild the workflow definition if variable IDs changed after edits or import.
  4. Declare the variable at the workflow definition level if it must be reachable from multiple branches.

Example fix

// before
// SetVariable.Variable references variable "MyVar" (id=xyz) living in a sibling composite scope -> throws
// after
// Declare MyVar on the workflow root (or ancestor container) so EnumerateVariablesInScope() includes it,
// then re-link SetVariable.Variable to the root-scoped variable.
Defensive patterns

Strategy: validation

Validate before calling

// verify the SetVariable target is in scope before running
var variableId = setVariable.Variable?.Id;
var inScope = context.ExpressionExecutionContext.EnumerateVariablesInScope().Any(v => v.Id == variableId);
if (!inScope)
    throw new InvalidOperationException($"Variable '{variableId}' is not in scope for this SetVariable activity.");

Type guard

static bool IsVariableInScope(ActivityExecutionContext context, Variable? variable) =>
    variable != null && context.ExpressionExecutionContext.EnumerateVariablesInScope().Any(v => v.Id == variable.Id);

Try / catch

try { await workflowRunner.RunAsync(workflow); }
catch (InvalidOperationException ex) when (ex.Message.Contains("not found") && ex.Message.Contains("Variable"))
{
    logger.LogError(ex, "SetVariable references an out-of-scope variable");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of elsa-workflows/elsa-core@fe9217bdfa (2026-09-13). Data as JSON: /api/errors/39b31fa487a0c304. Report an issue: GitHub.

Appendix: source

Thrown at src/modules/Elsa.Workflows.Core/Activities/SetVariable.cs:108

    /// </summary>
    [Input(Description = "The variable to assign the value to.")]
    public Variable? Variable { get; set; }

    /// <summary>
    /// The value to assign.
    /// </summary>
    [Input(Description = "The value to assign.")]
    public Input<object?> Value { get; set; } = new(default(object));

    /// <inheritdoc />
    protected override void Execute(ActivityExecutionContext context)
    {
        // Always refer to the variable by ID to ensure that the variable is resolved from the correct scope.
        var variableId = Variable?.Id; 
        var variable = context.ExpressionExecutionContext.EnumerateVariablesInScope().FirstOrDefault(x => x.Id == variableId);
     
        if (variable == null)
            throw new InvalidOperationException($"Variable '{variableId}' not found.");
        
        var value = context.Get(Value);
        variable.Set(context, value);
    }
}

View on GitHub (pinned to fe9217bdfa)