# Variables scope

**URL:** <https://forum.flowable.org/t/variables-scope/765>\
**Category:** Flowable Engine\
**Created:** [July 24, 2017, 2:03pm UTC](https://forum.flowable.org/t/variables-scope/765 "2017-07-24T14:03:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aeilers](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@aeilers](https://forum.flowable.org/u/aeilers)\
**Post date:** [July 24, 2017, 2:03pm UTC](https://forum.flowable.org/t/variables-scope/765/1 "2017-07-24T14:03:41Z")

</div>

Hi,

I encountered a strange behaviour:

When I add variables to a task and complete the whole process, I can no longer see the variables on that task. Based on the [DOCS](http://www.flowable.org/docs/userguide/index.html#apiVariables) this should not happen as long as the variables are not local.

The HistroyService shows me the variables when querying them from the process

```
historyService.createHistoricVariableInstanceQuery().processInstanceId(simpleProcess.getId()).list();

```

but not when I query them from the task

```
historyService.createHistoricVariableInstanceQuery().taskId(task1.getId()).list();

```

To explain the problem a little more, I wrote a [simple test case](https://gitlab.edvsz.hs-osnabrueck.de/aleeiler/FlowableScope/blob/f8a763586204aa54c3b5f7d3f3277953efca5292/src/test/java/de/hsos/ehealth/FlowableScope/FlowableScopeApplicationTests.java). I expect that all five test succeed. However the last three, where I try to get the variables from the tasks, fail.

//Update:  
[I fixed it](https://gitlab.edvsz.hs-osnabrueck.de/aleeiler/FlowableScope/blob/1126867a90983ff1683158ba0e24cccf8e436a94/src/test/java/de/hsos/ehealth/FlowableScope/HistoricVariableTests.java) by using `setVariableLocal`. However in this case I don’t understand why the variables are visible in the process scope?

---

<div class="post-metadata">

**Author:** ![joram](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/joram/32/26_2.png) [@joram](https://forum.flowable.org/u/joram)\
**Post date:** [July 25, 2017, 8:33am UTC](https://forum.flowable.org/t/variables-scope/765/2 "2017-07-25T08:33:30Z")

</div>

The logic is as you’ve described in your update:

- by default, variables are set on the process instance level
- if you set variables local (task/execution), they get a reference to that local task or execution. However, the variable is always enriched with the process instance id (if applicable, not for standalone tasks) for query purposes.

When a .getVariable() is done, the local scope is always checked first, before going upwards in scopes and eventually ending up at the process instance level.

---

<div class="post-metadata">

**Author:** ![aeilers](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@aeilers](https://forum.flowable.org/u/aeilers)\
**Post date:** [July 25, 2017, 8:37am UTC](https://forum.flowable.org/t/variables-scope/765/3 "2017-07-25T08:37:43Z")

</div>

Thanks for the explanation.
