# Helping with subflows and context access examples

**URL:** <https://discourse.nodered.org/t/helping-with-subflows-and-context-access-examples/54534>\
**Category:** Share Your Projects\
**Created:** [30 November 2021 21:17 UTC](https://discourse.nodered.org/t/helping-with-subflows-and-context-access-examples/54534 "2021-11-30T21:17:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 November 2021 21:17 UTC](https://discourse.nodered.org/t/helping-with-subflows-and-context-access-examples/54534/1 "2021-11-30T21:17:27Z")

</div>

To _put it out there_ here is a flow with 2 examples of `subflow` and accessing context.

The first is accessing context from within the `subflow`.

The second accesses context from the _parent_ flow.

This example READS context. To WRITE you would only need to reverse the commands from reading to writing. (get/put)

To set the `subflow` context in the first example, double click on the `subflow` icon and set the variable/s there.

 ![Screenshot from 2021-12-01 08-13-02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/400decd591b3e19a726cf141d1105f8136d41556.png)

The second example press the `set flow variable` first.  
Then press the node button below to make the subflow read the flow's context.

```auto
[{"id":"7d297bcb6d394137","type":"subflow","name":"EXAMPLE 2","info":"","category":"","in":[{"x":90,"y":80,"wires":[{"id":"0e60e8c38370930d"}]}],"out":[{"x":310,"y":80,"wires":[{"id":"0e60e8c38370930d","port":0}]}],"env":[],"meta":{},"color":"#DDAA99"},{"id":"0e60e8c38370930d","type":"function","z":"7d297bcb6d394137","name":"","func":"var my_variable = flow.get(\"$parent.example\");\n\nmsg.payload = my_variable;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":200,"y":80,"wires":[[]]},{"id":"3a93e082.e06f8","type":"subflow","name":"EXAMPLE","info":"","category":"","in":[{"x":40,"y":160,"wires":[{"id":"7eba2b65.f1ecd4"},{"id":"3cdbaf03.01d94"},{"id":"d7956efa.6d90b"}]}],"out":[{"x":680,"y":100,"wires":[{"id":"7eba2b65.f1ecd4","port":0}]},{"x":680,"y":160,"wires":[{"id":"3cdbaf03.01d94","port":0}]},{"x":680,"y":280,"wires":[{"id":"d7956efa.6d90b","port":0}]}],"env":[],"meta":{},"color":"#DDAA99"},{"id":"7eba2b65.f1ecd4","type":"change","z":"3a93e082.e06f8","name":"set msg.payload to sub-flow \"some_var\" parameter in a change node","rules":[{"t":"set","p":"payload","pt":"msg","to":"some_var","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":100,"wires":[[]]},{"id":"3cdbaf03.01d94","type":"function","z":"3a93e082.e06f8","name":"set msg.payload to sub-flow \"some_var\" parameter in a function node","func":"msg.payload = env.get(\"some_var\");\n\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":160,"wires":[[]]},{"id":"d7956efa.6d90b","type":"change","z":"3a93e082.e06f8","name":"set msg.payload to sub-flow \"some_var\" parameter in a change node with JSONata expressions","rules":[{"t":"set","p":"payload","pt":"msg","to":"$env('some_var')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":220,"wires":[[]]},{"id":"ae95f225.51856","type":"subflow:3a93e082.e06f8","z":"1747bdb12f3df9d0","name":"test sub flow var","env":[{"name":"some_var","value":"not this one","type":"str"}],"x":300,"y":260,"wires":[["e8220033.5a99e"],["536c2dc2.21f7a4"],["d730a55a.546418"]]},{"id":"e06bc9da.7e5ac8","type":"inject","z":"1747bdb12f3df9d0","name":"just a manual trigger","props":[{"p":"payload","v":"","vt":"str"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":290,"y":200,"wires":[["ae95f225.51856"]]},{"id":"e8220033.5a99e","type":"debug","z":"1747bdb12f3df9d0","name":"Change Node >>> Select $ env variable and type some_var in the field","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":710,"y":200,"wires":[]},{"id":"536c2dc2.21f7a4","type":"debug","z":"1747bdb12f3df9d0","name":"Function Node >>> let/var foo = env.get(\"some_var\");","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":660,"y":260,"wires":[]},{"id":"d730a55a.546418","type":"debug","z":"1747bdb12f3df9d0","name":"JSONata >>> $env('some_var')","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":590,"y":320,"wires":[]},{"id":"b4863e65.35379","type":"comment","z":"1747bdb12f3df9d0","name":"Subflow Environment Variable settings *","info":"On the `subflow` node: \n* Click edit properties button\n * At the bottom of Environment Variables Tab click the +add button\n * A new row with 2 fields (Name & Default Value) shall appear within the Environement Variables Tab\n * In the Name Field... type your variable name (example: 'some_var')\n * Now you can use add 'some_var' throughtout the subflow.\n * Any values typed into the 'some_var' parameter field, will be used wherever 'some_var' variable is used within the subflow.","x":230,"y":320,"wires":[]},{"id":"8a9e7f63766fa42e","type":"comment","z":"1747bdb12f3df9d0","name":"Get Subflow variables from parent flow","info":"","x":470,"y":140,"wires":[]},{"id":"dc1a1c3b66ff7585","type":"comment","z":"1747bdb12f3df9d0","name":"Access flow variables in subflow.","info":"","x":500,"y":430,"wires":[]},{"id":"a88c04c0743eae1d","type":"change","z":"1747bdb12f3df9d0","name":"set `flow.example` with `msg.payload`","rules":[{"t":"set","p":"example","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":470,"wires":[[]]},{"id":"3a7947469363c259","type":"inject","z":"1747bdb12f3df9d0","name":"Set flow variable","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"example message","payloadType":"str","x":250,"y":470,"wires":[["a88c04c0743eae1d"]]},{"id":"e93ac63fe1624162","type":"inject","z":"1747bdb12f3df9d0","name":"Just a trigger signal","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":260,"y":520,"wires":[["ea090d89e3973113"]]},{"id":"ea090d89e3973113","type":"subflow:7d297bcb6d394137","z":"1747bdb12f3df9d0","name":"Get parent flow context","env":[],"x":560,"y":520,"wires":[["d863c884443c14e7"]]},{"id":"d863c884443c14e7","type":"debug","z":"1747bdb12f3df9d0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":820,"y":520,"wires":[]}]

```
