# Multiple subflows with context variables in a tab

**URL:** https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916
**Category:** General
**Created:** [12 September 2021 08:56 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916 "2021-09-12T08:56:12Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [12 September 2021 08:56 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916/1 "2021-09-12T08:56:12Z")

</div>

Hello,  
I have a little problem understanding & figuring out how I can have multiple subflows on the same tab flow where each subflow needs to have it's own variables in context

As example, I have this test flow below. The only thing the subflow does is to set a flow context variable with the timestamp. But if I put more subflows in the same way into a single tab, there is a conflict

Is there another way how each subflow can store individual variables in context instead of using $parent.timestamp as in the example? Maybe like adding the unique node\_id to the variable names somehow could be a solution ($parent.timestamp + node\_id)?

Best regards, Walter

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e72d7fccb9f601e52a3c5d7fb3b79e415819a6e7.png)

```auto
[{"id":"598467b2.d6e028","type":"subflow","name":"My timestamp","info":"","category":"","in":[{"x":220,"y":80,"wires":[{"id":"2f923787.1c4268"}]}],"out":[{"x":620,"y":80,"wires":[{"id":"2f923787.1c4268","port":0}]}],"env":[],"meta":{},"color":"#DDAA99"},{"id":"2f923787.1c4268","type":"change","z":"598467b2.d6e028","name":"","rules":[{"t":"set","p":"$parent.timestamp","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":80,"wires":[[]]},{"id":"ebd92723.099598","type":"inject","z":"472fc173.1567","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":340,"y":1380,"wires":[["aba3db61.9fd578"]]},{"id":"7789d431.011d4c","type":"debug","z":"472fc173.1567","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":1380,"wires":[]},{"id":"aba3db61.9fd578","type":"subflow:598467b2.d6e028","z":"472fc173.1567","name":"","env":[],"x":560,"y":1380,"wires":[["7789d431.011d4c"]]}]

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [12 September 2021 09:11 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916/2 "2021-09-12T09:11:45Z")

</div>

I believe you can use flow context in a subflow and then each subflow will have its own copy.

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [12 September 2021 09:15 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916/3 "2021-09-12T09:15:06Z")

</div>

I forgot mention that I also need to make the variables accessible to other nodes in the same tab

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [12 September 2021 09:22 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916/4 "2021-09-12T09:22:55Z")

</div>

In that case you could use a subflow property to specify an id which you add as a suffix to the parent flow context var to identify which subflow it relates to.

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [12 September 2021 09:30 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916/5 "2021-09-12T09:30:18Z")

</div>

Yes, what I thought, but I would like to avoid having to put that in manually, I would like to automate, make it a no-brainer. That's why I thought of using the node-id but I dunno know how??

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [12 September 2021 10:17 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916/6 "2021-09-12T10:17:27Z")

</div>

If it were automatic how would you know how to address the var when used in the parent?

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [12 September 2021 10:44 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916/7 "2021-09-12T10:44:15Z")

</div>

I plan to keep an array or so in the parent with those

---

<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: [12 September 2021 10:50 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916/8 "2021-09-12T10:50:41Z")

</div>

You can feed in a unique id say in msg.topic or \_msgid, and then merge the context store with a new object using JSONata, I found that it only works with globals  
e.g.

```auto
[{"id":"598467b2.d6e028","type":"subflow","name":"My timestamp","info":"","category":"","in":[{"x":220,"y":80,"wires":[{"id":"2f923787.1c4268"}]}],"out":[{"x":620,"y":80,"wires":[{"id":"2f923787.1c4268","port":0}]}],"env":[],"color":"#DDAA99"},{"id":"2f923787.1c4268","type":"change","z":"598467b2.d6e028","name":"","rules":[{"t":"set","p":"timestamp","pt":"global","to":"$merge([$globalContext(\"timestamp\"),{$$.topic:$$.payload}])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":80,"wires":[[]]},{"id":"a6f9391f.533ff","type":"subflow:598467b2.d6e028","z":"b779de97.b1b46","name":"","x":600,"y":2700,"wires":[["7789d431.011d4c"]]},{"id":"ebd92723.099598","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"uniqueid1","payload":"","payloadType":"date","x":400,"y":2640,"wires":[["a6f9391f.533ff"]]},{"id":"7789d431.011d4c","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":700,"y":2660,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [12 September 2021 10:57 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916/9 "2021-09-12T10:57:00Z")

</div>

Yes, I think that could work fine, I can use globals instead since they are unique,  
Thank you!!

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [12 September 2021 11:20 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916/10 "2021-09-12T11:20:22Z")

</div>

Are you sure you really need to use context? Alternatively you could pass the information out of the subflow, then it would be automatic. I tend to consider that I have failed if I have to use flow or global context.

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [12 September 2021 11:57 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916/11 "2021-09-12T11:57:47Z")

</div>

No, not sure 😉 Not yet, still too much of an experimental work in progress,,,

---

<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: [26 September 2021 11:58 UTC](https://discourse.nodered.org/t/multiple-subflows-with-context-variables-in-a-tab/50916/12 "2021-09-26T11:58:38Z")

</div>

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