# How can i mention my "ui-button" code in the function node when it is pressed?

**URL:** <https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819>\
**Category:** Dashboard\
**Created:** [8 July 2022 07:51 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819 "2022-07-08T07:51:43Z")\
**Posts on this page:** 14\
**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:** [8 July 2022 07:51 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/1 "2022-07-08T07:51:43Z")

</div>

Hello guys, i wanted to implement an "if "statement to execute the action once the button is pressed. By referring to my simple code below, other than using msg.payload or msg.topic , is there any other statement i can use to tell the program that the button has been pressed? I tried using " Button.isPressed()" , but it wouldnt work.. any suggestion?

if(Button.isPressed()=true)

msg.topic ="Yay"

else  
{

```
msg.topic = " Not working"

```

}  
return msg;

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [8 July 2022 08:24 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/2 "2022-07-08T08:24:08Z")

</div>

Pressing the button will start the flow, so setting the msg.topic to (for example) "buttonpressed" in the button node, and feeding that into a function node, you can then use;

```auto
if (msg.topic == "buttonpressed") {
do something
}

```

If you are then using an if/else statement, you will probably need to get those states from saved context first, to ensure that their values are available when the button is pressed.

---

<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:** [8 July 2022 08:36 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/3 "2022-07-08T08:36:03Z")

</div>

Hi. Thanks for the heads up. The thing is i am using OPC UA to obtain the data and thus my "msg.payload" & "msg.topic" are both occupied with existing data value. And that is why im seeking for other alternative term as to not overwrite the data in the msg.payload and topic.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [8 July 2022 11:55 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/4 "2022-07-08T11:55:46Z")

</div>

> [@JackJackTan414](#):
>
> and thus my "msg.payload" & "msg.topic" are both occupied with existing data value

```auto
if (msg.topic == "existingdata") {
do something
} else if (msg.topic == "buttonpressed") {
do something different 
}

```

---

<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:** [8 July 2022 12:32 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/5 "2022-07-08T12:32:31Z")

</div>

> [@JackJackTan414](#):
>
> my "msg.payload" & "msg.topic" are both occupied with existing data value

I thought the message was coming from a ui-button.

---

<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:** [12 July 2022 00:39 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/6 "2022-07-12T00:39:58Z")

</div>

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 node. there is only 2 term here that can be delivered upon button pressed. is there any other term that i can use other than these two?

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

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

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

---

<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:** [12 July 2022 00:41 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/7 "2022-07-12T00:41:07Z")

</div>

msg.topic happens to be a variable generated via the OPC UA-client node.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [12 July 2022 16:15 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/8 "2022-07-12T16:15:21Z")

</div>

I would remove the button node from your flow, and use it to start a new 'stand alone' flow instead.  
The new flow would run when you press the button and retrieve the latest data from your database.

So the new flow would comprise;  
Button node - to run the flow  
Function node - to format the query  
Mariadb node - to retrieve the latest data

I don't use mariadb, so you'll have to do some searching to format the query.

---

<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 03:57 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/9 "2022-07-14T03:57:16Z")

</div>

hi Paul. Ive tried to create another flow(with button,function and debug node) mainly just to trigger the process in another flow. for button,Ive set the payload to be delivered to be a string('yes') when button is pressed.

Image 1 & 2 is the flow and the code respectively. this button on the dashboard, when pressed, it should trigger the process in other flow(shown in image 3 & 4).

The issue here is, i couldnt get the variable reading(image 3) that i have initialised in my other flow(image 1).

Im guessing there is some issue with my code.

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

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

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

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

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [14 July 2022 10:17 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/10 "2022-07-14T10:17:22Z")

</div>

Is this a different question to what you initially asked?  
I thought that you wanted to push a button, which would retrieve the last value saved to Maria.db?

I'm now confused what exactly that you are trying to achieve.

---

<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 11:24 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/11 "2022-07-14T11:24:46Z")

</div>

Looks like Jack has opened a second thread with the same question

> [@How to deliver variable value to different flow when ui-button is pressed](https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081):
>
> 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 reas…

@JackJackTan414 Please done post the same question more than once, it wastes people's time.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [14 July 2022 12:28 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/12 "2022-07-14T12:28:18Z")

</div>

A post was merged into an existing topic: [How to deliver variable value to different flow when ui-button is pressed](https://discourse.nodered.org/t/how-to-deliver-variable-value-to-different-flow-when-ui-button-is-pressed/65081/3)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [14 July 2022 12:29 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/13 "2022-07-14T12:29:23Z")

</div>

Topic closed because OP has duplicate post...

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [14 July 2022 12:29 UTC](https://discourse.nodered.org/t/how-can-i-mention-my-ui-button-code-in-the-function-node-when-it-is-pressed/64819/14 "2022-07-14T12:29:29Z")

</div>


