elsa-workflows/elsa-core · error · Exception

No parent variable container found

Error message

No parent variable container found

What it means

When setting a variable by name with SetVariable, the extension first looks for a predefined variable in scope; if none exists it tries to add a dynamic variable to the nearest ancestor that exposes a variable container. If no parent scope has an IVariableContainer at all, there is nowhere to store the variable and this exception is thrown.

Solutions

  1. Ensure the workflow (root activity) or an enclosing composite/container in the activity's ancestry declares a variable container.
  2. Wrap the scope in a Flowchart/Composite/Sequence (which provide variable containers) where the variable is set.
  3. Create the Variable explicitly and add it to the intended container rather than relying on dynamic scope resolution.
  4. Check that you are not calling the API on a stale or detached ActivityExecutionContext; obtain the context from a live workflow execution.

Example fix

// before
context.SetVariable("count", 42); // throws when no parent container
// after
var container = context.FindParentWithVariableContainer();
if (container == null)
    container = new Variable { Name = "count", Value = 42 }; // or ensure a Composite/Workflow ancestor declares variables
else
    context.SetVariable("count", 42);
Defensive patterns

Strategy: validation

Validate before calling

var container = context.FindParentWithVariableContainer();
if (container == null) throw new InvalidOperationException("No variable container in scope; wrap the activity in a workflow/composite that declares variables");

Type guard

bool hasContainer = context.FindParentWithVariableContainer() != null;

Try / catch

try { context.SetVariable(name, value); } catch (Exception ex) { logger.LogError(ex, "Cannot set variable {Name}: no variable container in scope", name); }

Prevention

When it happens

Trigger: Calling ActivityExecutionContext.SetVariable/GetOrCreateVariable-style APIs from a context whose ancestry contains no variable container — e.g. an activity executed outside any workflow/composite scope that owns variables, or a detached ActivityExecutionContext.

Common situations: Setting variables from custom activities running in a root context that lacks a VariableContainer; using variable APIs in background/scheduled work where the execution context is no longer attached to a workflow; misconfigured composites that do not declare variables.

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/e39a170a703bf0f5. Report an issue: GitHub.

Appendix: source

Thrown at src/modules/Elsa.Workflows.Core/Extensions/ActivityExecutionContextExtensions.cs:119

        public Variable SetVariable(string name, object? value, Action<MemoryBlock>? configure = null) =>
            context.ExpressionExecutionContext.SetVariable(name, value, configure);

        public Variable SetDynamicVariable<T>(string name, T value, Action<MemoryBlock>? configure = null)
        {
            // Check if a predefined variable already exists.
            var predefinedVariable = context.ExpressionExecutionContext.GetVariable(name);

            if (predefinedVariable != null)
            {
                context.SetVariable(name, value);
                return predefinedVariable;
            }

            // No predefined variable exists, so we will add a dynamic variable to the current container in scope.
            var container = context.FindParentWithVariableContainer();

            if (container == null)
                throw new("No parent variable container found");

            var existingVariable = container.DynamicVariables.FirstOrDefault(x => x.Name == name);

            if (existingVariable == null)
                container.DynamicVariables.Add(new Variable<T>(name, value));

            return context.SetVariable(name, value);
        }

        /// <summary>
        /// Gets a workflow variable by name.
        /// </summary>
        /// <param name="name">The name of the variable.</param>
        /// <typeparam name="T">The type of the variable.</typeparam>
        /// <returns>The variable if found, otherwise null.</returns>
        public T? GetVariable<T>(string name) => context.ExpressionExecutionContext.GetVariable<T?>(name);

        /// <summary>

View on GitHub (pinned to fe9217bdfa)