# Onscreen notification when a table gets updated

**URL:** <https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [September 20, 2022, 7:16pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302 "2022-09-20T19:16:54Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [September 20, 2022, 7:16pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/1 "2022-09-20T19:16:54Z")

</div>

Good afternoon. Is there a way to have notification from when a table get updated while not watching the table? I am making a messaging application and the way it work is: the user will select from a list of contacts, enter the subject and body and press send. Pic for details:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/a/2a2593ac133f04a85a9ed38f60408869c47b2f5e.png)  
This table has been filtered to show messages TO the logged in person. Still in testing phase thats why it looks like its messaging itself. There is also a 60 sec auto logout. As I cant guarantee this person will stay on this page Im looking for a way to let them know on another page that they have a new message.

Hope all this made sense

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [September 20, 2022, 8:17pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/2 "2022-09-20T20:17:39Z")

</div>

The problem you’re going to run into is that your Table can broadcast that there has been a change, but you need something consistent to _listen_ for that broadcast. Shared Docked Views are excellent for this (as long as they’re never overridden), but you need some piece of UI that is present on EVERY page for this to work correctly.

The general breakdown:  
Whenever the Table receives new data, you want to notify all pages of a session to display a “notification”, and I suggest you’ll also probably want to remove the notification after some time.

**Important pieces:**  
Change Script on `Table.props.data`  
Some sort of container/component which is always present on every page.

**1. Change Script for `Table.props.data`:**

```auto
# we need to send the pageId to omit the page which contains the Table
system.perspective.sendMessage("TABLEUPDATE", payload={"pageId":self.page.props.pageId}, scope="session")

```

**2. The TABLEUPDATE Session Message Handler, located on some sort of shared omni-present root/container/component:**

```auto
	from time import sleep
	pageId = self.page.props.pageId
	if pageId != payload["pageId"]:
	    system.perspective.openPopup(id=pageId, view="Alt", position={"right": 10, "top": 10}, showCloseIcon=False, pageId=pageId)
	    sleep(5) # adjust as desired
	    system.perspective.closePopup(id=pageId)

```

Idiosyncrasies to watch for:

- If the table updates during the five seconds the popup is open, then the popup will still be closed after the original five seconds have elapsed.

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [September 21, 2022, 12:38pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/3 "2022-09-21T12:38:35Z")

</div>

Thank you for the idea. I am going to try it today.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [September 21, 2022, 12:58pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/4 "2022-09-21T12:58:05Z")

</div>

How about a message handler in the session scripts ? Can’t something be done from there ?

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [September 21, 2022, 5:49pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/5 "2022-09-21T17:49:17Z")

</div>

> [@pascal.fragnoud](#):
>
> How about a message handler in the session scripts

The Session-level message handlers do not have a Perspective page thread attached to them (no page context because this is a Session event), so most `system.perspective` package functions will fail when executed in those handlers. The only thinsg I recommend doing within the Session-level handlers is sending a hand-off message to be hard by some part of an omni-present UI, or acting on the session object provided as part of the handler.

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [September 21, 2022, 8:31pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/6 "2022-09-21T20:31:27Z")

</div>

Im thinking I am going to make another table and populate it with a badgenum and a time stamp.

I have this as a script to poplulate that table:

```auto
	BadgeNum = '0038014279'
	LogInTime = system.date.now()
	LastLogIn = system.db.runScalarPrepQuery("SELECT 1 FROM New_Message_Alarm WHERE BadgeNum = ?", [BadgeNum], 'IgnitionMessagingUserForMessagingApp')
	if LastLogIn == 1:
		system.db.runPrepUpdate("INSERT INTO New_Message_Alarm WHERE BadgeNum = ? (LastLogIn) Values(?)",[BadgeNum, LogInTime])
	else:
		system.db.runPrepUpdate("INSERT INTO New_Message_Alarm (BadgeNum, LastLogIn) Values(?,?)",[BadgeNum, LogInTime])

```

It says I have incorrect syntax by the key word WHERE on the runPrepUpdate at the if statment. What am I missing?

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [September 21, 2022, 8:41pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/7 "2022-09-21T20:41:44Z")

</div>

Perhaps I am confused, but if the “table” sends a message why can’t a session message handler pick it up and open a popup to display your notification? Any running session will get this notification.

I know I have done this before, just not 100% on how I did it now.

Edit, yes I think I was sending a Page Id as part of the payload.

---

<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:** [September 21, 2022, 8:54pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/8 "2022-09-21T20:54:31Z")

</div>

SQL Inserts don’t accept WHERE clauses themselves, unless you use a SELECT instead of VALUES.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [September 21, 2022, 9:06pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/9 "2022-09-21T21:06:08Z")

</div>

> [@jlandwerlen](#):
>
> if the “table” sends a message why can’t a session message handler pick it up and open a popup to display your notification?

Because `system.perspective.openPopup()` requires a page thread in order to execute, but the Session Message Handler (`Project > Session Events > Message`) is just operating blindly on the Gateway - with only the `session` object supplied.

 ![Screen Shot 2022-09-21 at 1.52.49 PM](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/4/44131e47e79a7af64a659b9b6aeda7788eb91316.png)

Executing that Message Handler results in the following exception:

```auto
java.lang.IllegalArgumentException: No perspective page attached to this thread.

```

You could modify the code to supply the pageId argument, but you'd need to either send those as part of the payload or obtain them in the Message Handler and then omit the page with the table by passing only that page as part of the payload:

```auto
def handleMessage(session, payload):
	for sessionObj in system.perspective.getSessionInfo():
		if sessionObj["sessionScope"] != "designer":
			for pageId in sessionObj["pageIds"]:
				system.perspective.openPopup("X", "View", pageId=pageId)

```

```auto
def handleMessage(session, payload):
	for sessionObj in system.perspective.getSessionInfo():
		if sessionObj["sessionScope"] != "designer":
			for pageId in sessionObj["pageIds"]:
				if pageId != payload["originPageId"]:
					system.perspective.openPopup("X", "View", pageId=pageId)

```

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [September 21, 2022, 9:11pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/10 "2022-09-21T21:11:30Z")

</div>

> [@cmallonee](#):
>
> but you'd need to either send those as part of the payload

Yep, you must have missed my edit to my post.

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [September 22, 2022, 1:09pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/11 "2022-09-22T13:09:31Z")

</div>

May I get an example of this? Thank you

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [September 22, 2022, 2:43pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/12 "2022-09-22T14:43:59Z")

</div>

This worked:

```auto
system.db.runPrepUpdate("UPDATE New_Message_Alarm SET LastLogIn = ? WHERE BadgeNum = ?", [LogInTime, BadgeNum])

```

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [September 23, 2022, 4:20pm UTC](https://forum.inductiveautomation.com/t/onscreen-notification-when-a-table-gets-updated/64302/13 "2022-09-23T16:20:24Z")

</div>

Incase you wanna to see the final result here it is:

```auto
BadgeNum = self.session.custom.View_Keyboard
	LogInTime = system.date.now()
	LastLogIn = system.db.runScalarPrepQuery("SELECT 1 FROM New_Message_Alarm WHERE BadgeNum = ?", [BadgeNum], 'IgnitionMessagingUserForMessagingApp')	
	if LastLogIn == 1:
		LLTS = system.db.runScalarPrepQuery("SELECT LastLogIn FROM New_Message_Alarm WHERE BadgeNum = ?", [BadgeNum], 'IgnitionMessagingUserForMessagingApp') #Looks at a table to see when the last time someone logged in
		LMTS = system.db.runScalarPrepQuery("SELECT TOP 1 [Time:] FROM Message_Table WHERE [To:] = ? ORDER BY [Time:] DESC",[BadgeNum], 'IgnitionMessagingUserForMessagingApp')	#Looks at message table to see when last message was sent to that BadgeNum
		system.db.runPrepUpdate("UPDATE New_Message_Alarm SET LastLogIn = ? WHERE BadgeNum = ?", [LogInTime, BadgeNum]) #Updates LastsLogIn with timestamp
		if LLTS < LMTS: #Compares the last login timestamp to the new message timestamp
			self.getSibling("NewMessage_Label").meta.visible = True
		else:
			self.getSibling("NewMessage_Label").meta.visible = False	
	else:
		system.db.runPrepUpdate("INSERT INTO New_Message_Alarm (BadgeNum, LastLogIn) Values(?,?)",[BadgeNum, LogInTime])

```
