# Write to DB with Save Button Script

**URL:** <https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960>\
**Category:** Ignition\
**Created:** [November 15, 2023, 9:38pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960 "2023-11-15T21:38:32Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcan87](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dcan87/32/54071_2.png) [@dcan87](https://forum.inductiveautomation.com/u/dcan87)\
**Post date:** [November 15, 2023, 9:38pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/1 "2023-11-15T21:38:32Z")

</div>

```auto
def runAction(self, event):
	# Grab the values to place in new DB row.
	Parent = self.parent.parent.parent.getChild("containerLineMachine").getChild("labelLineName").props.text
	SAP = self.parent.parent.parent.getChild("containerSapLabel").getChild("SAP Label").props.text
	CommonName = self.parent.parent.parent.getChild("containerLineMachine").getChild("labelCommonName").props.text
	Note = self.parent.parent.getChild("CoordinateContainer").getChild("NewCommentTextArea").props.text
	 
	# Use those values in the Insert Comment Named Query.
	system.db.runNamedQuery("InsertMachineComment", {"myParent":Parent, "mySAP":SAP, "myCommonName":CommonName, "myNote":Note})
	 
	# Refresh the table to immediately bring in the new row.
	system.db.refresh(event.source.parent.getChild("CommentHistoryTable"), "data")

```

Ok, what am I doing wrong here?

On Click of the 'Save' button appears to do nothing. This is an event script onActionPerformed on my 'Save' button.

---

<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:** [November 15, 2023, 9:42pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/2 "2023-11-15T21:42:32Z")

</div>

> [@Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831):
>
> This thread is intended as a short help file for all those struggling to post formatted code on the forum. Posting formatted code on the forum (and most others) is as simple as selecting the code and pressing the \</\> button. It works the same as applying Bold or Italic styles to your document. It preserves indentation and applies syntax highlighting. [How to post code]

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [November 15, 2023, 9:43pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/3 "2023-11-15T21:43:09Z")

</div>

> [@dcan87](#):
>
> Ok, what am I doing wrong here?

Posting a picture of code without properly formatted text following in the text?

On a serious note though:

I see from your code that you are working in perspective.

`system.db.refresh()` is only scoped for vision. You're looking for `self.refreshBinding()`

P.S. I'm not retyping your script.

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [November 15, 2023, 9:48pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/4 "2023-11-15T21:48:54Z")

</div>

If you look at the logs in the gateway there is a good chance it will tell you exactly what the problem is, or at least point you to the line in fault.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/d/bd588035634b1235ea2eb07f50c630ac90b06e7f.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:** [November 15, 2023, 9:52pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/5 "2023-11-15T21:52:06Z")

</div>

Tip: a handy way to debug a script is,

- Create a memory tag, say, `projectNameDebug`.
- Where you need to debug in your script add in a write to the tag:

`system.tag.writeBlocking(["[default]projectNameDebug"], ["onActionPerformed fired."]`

This will show you the last debug before the script stopped.

[https://docs.inductiveautomation.com/display/DOC81/system.tag.writeBlocking](https://docs.inductiveautomation.com/display/DOC81/system.tag.writeBlocking)

---

<div class="post-metadata">

**Author:** ![dcan87](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dcan87/32/54071_2.png) [@dcan87](https://forum.inductiveautomation.com/u/dcan87)\
**Post date:** [November 15, 2023, 9:56pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/6 "2023-11-15T21:56:52Z")

</div>

> [@lrose](#):
>
> self.refreshBinding()

Good insight thank you, I will start creating this habit!

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [November 15, 2023, 11:00pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/7 "2023-11-15T23:00:53Z")

</div>

> [@Transistor](#):
>
> Tip: a handy way to debug a script is,
> 
> - Create a memory tag, say, `projectNameDebug`.
> - Where you need to debug in your script add in a write to the tag:
> 
> `system.tag.writeBlocking(["[default]projectNameDebug"], ["onActionPerformed fired."]`

Not discrediting this by any means, but why would you do this when you can use a logger to log to the wrapper file, or for temporary debugging use `system.perspective.print()` (or just `print` for vision) and print messages directly to the output console?

---

<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:** [November 15, 2023, 11:05pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/8 "2023-11-15T23:05:28Z")

</div>

> [@lrose](#):
>
> but why would you do this when you can use a logger

I find loggers difficult to use. Switch to the browser, stop the live view, try and find the logger, scroll right across because it doesn't wrap.  
The tag can be monitored on the Tag Browser pane.

Maybe I've missed something in my learning Igniton but I've searched and never found a good tutorial on the logger. I have a tremendous fund of ignorance to draw from!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 15, 2023, 11:05pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/9 "2023-11-15T23:05:45Z")

</div>

One advantage of the writing-to-a-tag approach is you get feedback inside the designer; no need to open the gateway webpage. But it is definitely easier to get richer info via the logs.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [November 15, 2023, 11:19pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/10 "2023-11-15T23:19:37Z")

</div>

> [@Transistor](#):
>
> I have a tremendous fund of ignorance to draw from!

I share that trait.

I have this bookmarked:

> [@Event Configuration Print Out to debug](https://forum.inductiveautomation.com/t/event-configuration-print-out-to-debug/60091/5):
>
> It’s complicated slight_smile Starting from most local to least… In the script console, print will log directly to the output buffer on the right hand side. system.util.getLogger() will log to the named ‘Output Console’ Designer window. Bonus: A script invoked from here using system.util.invokeAsync will print to the regular output console, not the buffer on the right hand side. Anywhere else in the designer (except Perspective), print and system.util.getLogger will log to the ‘Out…

Of particular interest is the last part of that. Very helpful, since I almost always have the developer tools open when working in perspective.

> [@Event Configuration Print Out to debug](https://forum.inductiveautomation.com/t/event-configuration-print-out-to-debug/60091/5):
>
> `system.perspective.print` has a ‘scope’ argument. If unspecified or set to ‘client’, output will be sent to the _browser console_ wherever the session is running, and _not logged to the gateway_. If scope is set to ‘gateway’, then output is logged…to the gateway.

---

<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:** [November 16, 2023, 1:24am UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/11 "2023-11-16T01:24:51Z")

</div>

> [@Transistor](#):
>
> loggers difficult to use

Heh. I use a 2nd 4k monitor, and use `tail -f .../wrapper.log` from an SSH connection to the gateway.

Highly recommended (really).

---

<div class="post-metadata">

**Author:** ![Tim\_Carlton](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tim_carlton/32/52835_2.png) [@Tim\_Carlton](https://forum.inductiveautomation.com/u/Tim_Carlton)\
**Post date:** [November 16, 2023, 2:07pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/12 "2023-11-16T14:07:11Z")

</div>

Nice. My laptop seems to top out at an external 4k plus an external FHD. Even a 1900x1200 at 30Hz makes it wonky.

---

<div class="post-metadata">

**Author:** ![dcan87](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dcan87/32/54071_2.png) [@dcan87](https://forum.inductiveautomation.com/u/dcan87)\
**Post date:** [November 16, 2023, 7:22pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/13 "2023-11-16T19:22:03Z")

</div>

Ok I am struggling to get the table to refresh after save:

```auto
def runAction(self, event):
	# Grab the values to place in new DB row.
	Parent = self.parent.parent.parent.getChild("containerLineMachine").getChild("labelLineName").props.text
	SAP = self.parent.parent.parent.getChild("containerSapLabel").getChild("SAP Label").props.text
	CommonName = self.parent.parent.parent.getChild("containerLineMachine").getChild("labelCommonName").props.text
	Note = self.parent.parent.getChild("CoordinateContainer").getChild("NewCommentTextArea").props.text
	 
	# Use those values in the Insert Comment Named Query.
	system.db.runNamedQuery("Machine Comments Panel/InsertMachineComment", {"myParent":Parent, "mySAP":SAP, "myCommonName":CommonName, "myNote":Note})
	 
	# Refresh the table to immediately bring in the new row.
	system.refreshBinding(self.parent.parent.getChild("CommentHistoryTable").props.data)

	# Clear text field
	self.parent.parent.getChild("CoordinateContainer").getChild("NewCommentTextArea").props.text = ""

```

Gateway log tells me Line 12 "object has no attribute 'refreshBinding'"

---

<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:** [November 16, 2023, 7:38pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/14 "2023-11-16T19:38:29Z")

</div>

> [@dcan87](#):
>
> `system.refreshBinding(self.parent.parent.getChild("CommentHistoryTable").props.data)`

Try,  
`self.parent.parent.getChild("CommentHistoryTable").refreshBinding("props.data")`

[https://docs.inductiveautomation.com/display/DOC81/Perspective+Component+Methods#PerspectiveComponentMethods-RefreshingBindings](https://docs.inductiveautomation.com/display/DOC81/Perspective+Component+Methods#PerspectiveComponentMethods-RefreshingBindings)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [November 16, 2023, 8:30pm UTC](https://forum.inductiveautomation.com/t/write-to-db-with-save-button-script/81960/15 "2023-11-16T20:30:11Z")

</div>

`refreshBinding()` is a function on the component object it's self. It isn't a `system` function.

Assuming that this script is located on the table you are attempting to update, then all you should need is:

```auto
self.refreshBinding("props.data")

```

If not, then you will need to get the path to the table component and then call refresh binding, as @Transistor has shown.
