# How to deliver variable value to different flow when ui-button is pressed

**URL:** <https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081>\
**Category:** General\
**Tags:** function-node\
**Created:** [14 July 2022 09:05 UTC](https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081 "2022-07-14T09:05:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JackJackTan414](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@JackJackTan414](https://discourse.nodered.org/u/JackJackTan414)\
**Post date:** [14 July 2022 09:05 UTC](https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081/1 "2022-07-14T09:05:53Z")

</div>

Good day Guys. The first 3 image represents that I am trying to initialise a global variable "a" to be 1 whenever i press the button. And the value represented by variable "a" will be stored as "msg.test" and to be used in other FLOW.

The last 3 image shows the other flow that I am trying to obtain the variable "a" value into this flow.

But when i press the button on the dashboard, I want this flow to show its message on debug node right away WITHOUT clicking on the inject node.

P/S: The reason why im using global variable is because in my main flow, both the msg.topic and msg.payload is occupied with the constantly generated value from OPCUA client.

And i want it such that when i press the button, I want these data to be saved into MySQL database. Therefore, im doing some testing here to make sure that i can pass on the global variable instead of letting my msg.topic or msg.payload overwrite by the value sent from the "ui-button" when its pressed.

 ![image1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/32e4da21a301c1967fdc45aefa61334ef6569665.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/a/0a4ab8580b5e51e8cab38c59cce03f785cdf2916.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b68fbd9fdfb7106b09c08ccf28452a9238b1e09.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/54f2f7eb253c7da9be6dc632d31138517c9bc2dc.png)

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4e5ffcdf2b14cdfff163284b2623251dab0ad0b.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:** [14 July 2022 09:25 UTC](https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081/2 "2022-07-14T09:25:25Z")

</div>

A flow will not run until something triggers it. Having a `function` node at the start of a flow will never run the flow, a msg has to be fed into the `function` node.

Why not also have a `switch` node connected to the button. Have it check the 'topic' ad iff it is a yes have the optput of the switch go to a `link-out` node. Have the `link-out` node connect to `link-in` nodes connected to the flows you need to run.

---

<div class="post-metadata">

**Author:** ![JackJackTan414](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@JackJackTan414](https://discourse.nodered.org/u/JackJackTan414)\
**Post date:** [14 July 2022 12:23 UTC](https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081/3 "2022-07-14T12:23:49Z")

</div>

Sorry for the misunderstanding, As you can see from my overall flow here, the button node here is suppose to act like a gate which before it is pressed, no value will be able to pass through it to the function and save it to the MySQL data base.

please have a look at the code in this function node(after button node) , there is 1 line which consist both msg.topic and msg.payload.

Since button can only send msg.topic and msg.payload, lets say if i were to write the code like this:-

**if(msg.topic=='pressed')**  
**{**  
**msg.topic = "INSERT INTO table1(`value`)VALUES(' " + msg.payload + " ');";**

**}**

**return msg;**

i wont get the value that i desired if i do if(msg.topic=='pressed') condition, my value will become a random string number in mySQL database

or the value turns out to be just "pressed" if i use if(msg.payload=="pressed") condition.

However, if i were to remove this button node, everything works fine.

This is my concept,I am planning to implement a dashboard whereby, if the end-user wants to record this reading, they just have to press the button and the data will be saved in a MYSQL database.

Once again, so sorry for causing a confusion.

Please refer to the image shown in this link.

> [@How can i mention my "ui-button" code in the function node when it is pressed?](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/6):
>
> Hi, sorry for the misunderstanding. The msg.topic contains "variable" values that is generated through the OPC-UA client node. And i would like it to display the data on the MySQL database only when i press the button on the dashboard. The second image shows the code im trying to implement in the function node after the button node. This is what i meant by both the "msg.topic" and "msg.payload" are pre-occupied with values. The third image is part of the configuration page of the ui-button nod…

---

<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:** [14 July 2022 12:55 UTC](https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081/4 "2022-07-14T12:55:45Z")

</div>

> [@JackJackTan414](#):
>
> This is my concept,I am planning to implement a dashboard whereby, if the end-user wants to record this reading, they just have to press the button and the data will be saved in a MYSQL database.

So just have the button attached to the nodes that builds the query and runs it. I don't see what is so hard about this.  
 ![Screen Shot 2022-07-14 at 8.55.13 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9b98908569e83ce5c53cbf05bc36a60ea95c4d51.png)

---

<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:** [14 July 2022 12:58 UTC](https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081/5 "2022-07-14T12:58:29Z")

</div>

Or do you mean that you want to allow messages through or block them, by using a button? If so then have a look at node-red-contrib-simple-gate which can do that.

---

<div class="post-metadata">

**Author:** ![JackJackTan414](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@JackJackTan414](https://discourse.nodered.org/u/JackJackTan414)\
**Post date:** [14 July 2022 13:17 UTC](https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081/6 "2022-07-14T13:17:50Z")

</div>

my primary concern is that , this button can only send msg.topic & msg.payload value right?

But both of these (msg.topic & msg.payload) exist in my function node(after the button node) as shown below:-

**msg.topic = "INSERT INTO table1(`value`) VALUES( ' " + msg.payload + " ');";"**

So lets say if i were to use "if" condition , i can only choose either to send Payload or Topic value when it is pressed. Which is what i dont want as this will overwrite the data in msg.payload and msg.topic that is to be stored into MySQL database.

---

<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:** [14 July 2022 13:57 UTC](https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081/7 "2022-07-14T13:57:15Z")

</div>

I’m building an inventory system. On the input page in the dashboard I’ll enter data in some ui-text nodes. Each node sends its msg to a change node that stored it in a flow variable.

When the button is pressed a msg is passed to a change node. The change node will move all the flow variables to msg.payload as a object. That is passed to a template node that builds the query and puts it into msg.topic and then it is sent to the sql node.

---

<div class="post-metadata">

**Author:** ![JackJackTan414](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@JackJackTan414](https://discourse.nodered.org/u/JackJackTan414)\
**Post date:** [14 July 2022 14:12 UTC](https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081/8 "2022-07-14T14:12:47Z")

</div>

Hi, thanks for your suggestion. I will give it a try when I get to my office tomorrow. Sorry I am running on on asia time zone here. I will update again tomorrow.

---

<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:** [12 September 2022 14:13 UTC](https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081/9 "2022-09-12T14:13:23Z")

</div>

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