# How to trigger SQL Query on request?

**URL:** <https://forum.inductiveautomation.com/t/how-to-trigger-sql-query-on-request/88775>\
**Category:** Maker\
**Tags:** ignition81, perspective\
**Created:** [April 13, 2024, 12:19pm UTC](https://forum.inductiveautomation.com/t/how-to-trigger-sql-query-on-request/88775 "2024-04-13T12:19:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vidar\_Hetlelid](https://avatars.discourse-cdn.com/v4/letter/v/dfb087/32.png) [@Vidar\_Hetlelid](https://forum.inductiveautomation.com/u/Vidar_Hetlelid)\
**Post date:** [April 13, 2024, 12:19pm UTC](https://forum.inductiveautomation.com/t/how-to-trigger-sql-query-on-request/88775/1 "2024-04-13T12:19:17Z")

</div>

Hi there,

I have a table that gets data from SQL query. But instead of polling evey X second, I want to run the query on reqest. Like when page is loaded, and when i add data using the form below the table. How can this be accieved? Thanks 🙂

Any other tips and tricks for how to make this user friendly is vey appreciated.

Screen shot of table and how users can add/update data.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/e/5ebd1269352517caeeab17bff07fba7ed1d16572.png)

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 13, 2024, 12:43pm UTC](https://forum.inductiveautomation.com/t/how-to-trigger-sql-query-on-request/88775/2 "2024-04-13T12:43:25Z")

</div>

The standard procedure is:

- Create the SQL binding on the table's `data` property.
- On load the binding will be automatically refreshed and the table will update.
- Your Oppdater button will insert or update the database by script - probably using an onActionPerformed1 event.
- At the end of the script you should use a message handler.  
`system.perspective.sendMessage("updateTableDatasource", payload = {}, scope = "view")`
- On the Table component, right-click | Configure Scripts | Message Handlers | + Add Handler. Set up as shown below.

 ![Table refresh binding](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/5/e57af7d4392a5a736abffabdb24d925a08e480be.png)

`self.refreshBinding('props.data')`

* * *

1 onClick will be triggered even if the button is not enabled.  
onActionPerformed event will not occur if the button is not enabled.

> **[system.perspective.sendMessage | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-perspective/system-perspective-sendMessage)**
>
> Send a message to a message handler within the same session.

Nice elegant form design! 👏

---

<div class="post-metadata">

**Author:** ![Vidar\_Hetlelid](https://avatars.discourse-cdn.com/v4/letter/v/dfb087/32.png) [@Vidar\_Hetlelid](https://forum.inductiveautomation.com/u/Vidar_Hetlelid)\
**Post date:** [April 13, 2024, 12:55pm UTC](https://forum.inductiveautomation.com/t/how-to-trigger-sql-query-on-request/88775/3 "2024-04-13T12:55:58Z")

</div>

Thank you so much! That worked so damn smooth 🙂

---

<div class="post-metadata">

**Author:** ![Vidar\_Hetlelid](https://avatars.discourse-cdn.com/v4/letter/v/dfb087/32.png) [@Vidar\_Hetlelid](https://forum.inductiveautomation.com/u/Vidar_Hetlelid)\
**Post date:** [April 13, 2024, 7:03pm UTC](https://forum.inductiveautomation.com/t/how-to-trigger-sql-query-on-request/88775/4 "2024-04-13T19:03:35Z")

</div>

Hi, I just ran into another problem regarding this. I wanted to display a popup in order to confirm before deleting from the table, so I made this little popup:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/f/3f926e5df5a5476e11d34accce14d21e6c26fdf7.png)

When pressing "Ja", this script is run:

```auto
def runAction(self, event):

	parameters = {
	"DeleteID":self.custom.key}
		
	system.db.runNamedQuery("Todo/Todo_Delete", parameters)
	system.perspective.closePopup("CTILi51V")
	system.perspective.sendMessage("refresh_todo", payload = {}, scope = "view")
	

```

The script delete the row, and closes the popup, but wont refresh the table. Any ideas? 🙂 This "refresh\_todo" works when its not used in a popup, so I guess something needs to be done in order to pass it to the table or something...

---

<div class="post-metadata">

**Author:** ![Vidar\_Hetlelid](https://avatars.discourse-cdn.com/v4/letter/v/dfb087/32.png) [@Vidar\_Hetlelid](https://forum.inductiveautomation.com/u/Vidar_Hetlelid)\
**Post date:** [April 13, 2024, 7:16pm UTC](https://forum.inductiveautomation.com/t/how-to-trigger-sql-query-on-request/88775/6 "2024-04-13T19:16:50Z")

</div>

Hmm..., should any parameters be passed to the message handler on the table?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/4/143f36060767678aaf3267341b8c756f088e5aa2.png)

---

<div class="post-metadata">

**Author:** ![Vidar\_Hetlelid](https://avatars.discourse-cdn.com/v4/letter/v/dfb087/32.png) [@Vidar\_Hetlelid](https://forum.inductiveautomation.com/u/Vidar_Hetlelid)\
**Post date:** [April 13, 2024, 7:22pm UTC](https://forum.inductiveautomation.com/t/how-to-trigger-sql-query-on-request/88775/8 "2024-04-13T19:22:58Z")

</div>

Yes, I tried, and problem is the same 😕

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 13, 2024, 7:28pm UTC](https://forum.inductiveautomation.com/t/how-to-trigger-sql-query-on-request/88775/9 "2024-04-13T19:28:26Z")

</div>

A popup is its own view, so scope="view" certainly won't work. Use page scope.

---

<div class="post-metadata">

**Author:** ![Vidar\_Hetlelid](https://avatars.discourse-cdn.com/v4/letter/v/dfb087/32.png) [@Vidar\_Hetlelid](https://forum.inductiveautomation.com/u/Vidar_Hetlelid)\
**Post date:** [April 13, 2024, 7:31pm UTC](https://forum.inductiveautomation.com/t/how-to-trigger-sql-query-on-request/88775/10 "2024-04-13T19:31:28Z")

</div>

Thats it! Thank you 🙂

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 13, 2024, 7:35pm UTC](https://forum.inductiveautomation.com/t/how-to-trigger-sql-query-on-request/88775/11 "2024-04-13T19:35:58Z")

</div>

Yes, and I messed up on the earlier post. The parameters were to go to the named query, not the sendMessage. I'll delete that post.

I'm glad you got it working.
