# Subflows & accessing flow/global variables on a per subflow basis (NOT environment variables)

**URL:** <https://discourse.nodered.org/t/subflows-accessing-flow-global-variables-on-a-per-subflow-basis-not-environment-variables/51084>\
**Category:** General\
**Created:** [16 September 2021 00:52 UTC](https://discourse.nodered.org/t/subflows-accessing-flow-global-variables-on-a-per-subflow-basis-not-environment-variables/51084 "2021-09-16T00:52:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![GeorgeCastanza](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/georgecastanza/32/40766_2.png) [@GeorgeCastanza](https://discourse.nodered.org/u/GeorgeCastanza)\
**Post date:** [16 September 2021 00:52 UTC](https://discourse.nodered.org/t/subflows-accessing-flow-global-variables-on-a-per-subflow-basis-not-environment-variables/51084/1 "2021-09-16T00:52:31Z")

</div>

I've got a subflow that I'm reusing multiple times. I want to access flow/global variables that are created/modified by the subflow. I've successfully created the flow/global variable by using this code in the subflow.

_(As you can see, the global variable for each unique subflow is set by $$.topic)_

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/1/f19e7755f72b379d9813a76f2c667b8f48178e54.png)

How would I go about checking the status (true/false) of that flow/global variable in a **subflow**. I cant seem to figure it out, **on a per topic basis** (so it can live in the subflow nicely)?

---

<div class="post-metadata">

**Author:** ![GeorgeCastanza](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/georgecastanza/32/40766_2.png) [@GeorgeCastanza](https://discourse.nodered.org/u/GeorgeCastanza)\
**Post date:** [16 September 2021 02:36 UTC](https://discourse.nodered.org/t/subflows-accessing-flow-global-variables-on-a-per-subflow-basis-not-environment-variables/51084/2 "2021-09-16T02:36:39Z")

</div>

This seems to work, after a lot of trial and error, but there still is an error below.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/4/740ab1a5a5dde656ecbff0be18365557c0d5e6df.png)

The error (only seems to error first time).

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/00a79c9100068aba3cd097aa178dfb179ccb9df6.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 September 2021 08:53 UTC](https://discourse.nodered.org/t/subflows-accessing-flow-global-variables-on-a-per-subflow-basis-not-environment-variables/51084/3 "2021-09-16T08:53:46Z")

</div>

Why not just pass the value to the subflow in the msg? Just before going to the subflow node, add a change node to set msg.???? to the value you want and then in the subflow just get it from the msg.

If it needed updating in the subflow, add a change node just after the subflow to store the value back into the context variable.

Two extra nodes, but it should solve any issues.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [16 September 2021 12:04 UTC](https://discourse.nodered.org/t/subflows-accessing-flow-global-variables-on-a-per-subflow-basis-not-environment-variables/51084/4 "2021-09-16T12:04:55Z")

</div>

Try this syntax

```auto
$GlobalContext("motionActive." & $$.topic)

```

---

<div class="post-metadata">

**Author:** ![GeorgeCastanza](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/georgecastanza/32/40766_2.png) [@GeorgeCastanza](https://discourse.nodered.org/u/GeorgeCastanza)\
**Post date:** [16 September 2021 12:55 UTC](https://discourse.nodered.org/t/subflows-accessing-flow-global-variables-on-a-per-subflow-basis-not-environment-variables/51084/5 "2021-09-16T12:55:03Z")

</div>

Thanks, that's it! I appreciate the help.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 September 2021 12:55 UTC](https://discourse.nodered.org/t/subflows-accessing-flow-global-variables-on-a-per-subflow-basis-not-environment-variables/51084/6 "2021-09-30T12:55:54Z")

</div>

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