Parallel subprocess and local variables

I have a subprocess, inside a ScriptTask FEEL which set a “local” variable.
I seems that this variable:

  • is set as global of the whole process (not only the subprocess)
  • all subprocess have the same value (the userTask display item_plurial==“item2s”)

image
test(1).bpmn (7.8 KB)

Based on your description, you’re experiencing the expected behavior of Camunda’s variable scoping system, but there are ways to achieve true local variables in parallel subprocesses.

The Issue

By default in Camunda, variables are created in the root (global) scope and are visible everywhere in the process instance. When your FEEL script task sets a variable, it’s being merged into the global process scope, which is why:

  1. The variable appears global across the whole process
  2. All parallel subprocess instances share the same value (the last one to execute overwrites the others)

Solutions

Here are several approaches to achieve true local variables in your parallel subprocesses:

1. Use Output Mappings to Control Variable Scope

Add output mappings to your subprocess or script task to control which variables get propagated:

  • If you define output mappings, only the mapped variables are merged globally
  • Variables not included in output mappings remain local to that scope
  • If you want to keep a variable completely local, simply don’t include it in any output mapping

2. Use Input/Output Mappings for Multi-Instance

If you’re using multi-instance subprocesses:

  • Use input mappings to create per-instance local variables
  • Use output mappings to control what gets merged back
  • Consider using outputElement/outputCollection to safely collect results from each instance

3. Variable Naming Strategy

Use unique variable names per subprocess instance to avoid conflicts:

// Instead of setting "item_plural"
// Set something like "item_plural_" + instanceId

Recommended Approach

For your specific case with parallel subprocesses, I recommend:

  1. Add output mappings to your subprocess that only map the variables you actually want to be global
  2. Keep working variables (like item_plural) local by not including them in output mappings
  3. If you need the values later, collect them using a different approach (like an output collection)

Would you like me to help you implement any of these solutions? Could you also share:

  • Are you using multi-instance subprocesses or just parallel gateways?
  • What Camunda version are you using?
  • Do you need these variables to be accessible outside the subprocess later?

References:

Hi @faroni

This is related to the concept of variable propagation.
To address this, you need to define the variable item_plural within the scope of each instance of the multi-instance embedded sub-process. This can be achieved using input mappings as shown in the attached snippet.

https://docs.camunda.io/docs/components/concepts/variables/#variable-propagation

Yes, i can set an Input “local variable” for each subprocess.
But there, i set the variable inside the subprocess
And i can not set a “output local variable” in the script task

Perfect! You can control variable scope in script tasks using output mappings. Here’s exactly how to solve your problem:

Solution: Use Output Mappings on the Script Task

You can add output mappings directly to your FEEL script task to prevent the variable from becoming global:

Option 1: Keep Variable Completely Local

  1. In your script task properties, go to the Output mappings section
  2. Don’t add any mappings - this will keep all result variables local to the script task scope
  3. The item_plural variable will remain local to that subprocess instance

Option 2: Control What Gets Propagated

  1. Add output mappings to your script task
  2. Only map the variables you actually want to propagate upward
  3. Leave item_plural unmapped to keep it local

Option 3: Use Subprocess Output Mappings

Alternatively, you can add output mappings at the subprocess level:

  1. Let the script task create the variable normally
  2. Add output mappings to the subprocess itself
  3. Only map the variables you want to escape the subprocess scope

How It Works

  • Without output mappings: Variables from script tasks are merged into the global process scope (your current problem)
  • With output mappings: Variables are first set as local to the task/subprocess scope, and only explicitly mapped variables propagate upward
  • Empty output mappings: Nothing propagates - all variables stay local

Example Configuration

In your script task:

  • Result Variable: item_plural
  • Output Mappings: Leave empty (or map other variables you need globally, but not item_plural)

This way, each parallel subprocess instance will have its own local item_plural variable that doesn’t interfere with the others.

Try adding empty output mappings to your script task - this should solve your variable scoping issue!

References:

@faroni

When you set a variable in a script task, variable propagation rules apply: if a variable with the same name already exists in the narrower scope, it will be updated there. If it doesn’t exist (like in your case), the variable gets created in the root/global scope.

In your scenario, you need the variable to be set within the scope of each instance of the multi-instance sub-process. This can be achieved using input mappings, which ensures a separate variable is created for each instance, holding the correct value set by the script task specific to that instance

it works but not “intuitive” :slight_smile: thank you

1 Like

Great! I’m glad the output mappings solution worked for you!

You’re absolutely right that it’s not very intuitive - Camunda’s variable scoping behavior can be surprising at first. The key takeaway is:

By default, variables created in script tasks become global, but you can use empty output mappings to keep them local to their scope.

This pattern is useful whenever you need working variables that shouldn’t interfere between parallel executions. Thanks for the feedback about the intuitiveness - it’s definitely one of those Camunda quirks that takes some getting used to!

Feel free to reach out if you run into any other variable scoping challenges. :slightly_smiling_face:

@zee-bot please, stop spamming et stop replying to me.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.