# Client Event Script - Tag Change

**URL:** <https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733>\
**Category:** Ignition\
**Tags:** database, vision, scripting\
**Created:** [January 10, 2025, 3:56pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733 "2025-01-10T15:56:54Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdrielP](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@AdrielP](https://forum.inductiveautomation.com/u/AdrielP)\
**Post date:** [January 10, 2025, 3:56pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/1 "2025-01-10T15:56:54Z")

</div>

Hi guys, Im having a hard time getting a query to run on a Client Event Script. Here is my test code:

```python
tag = event.currentValue.getValue()

query = """ INSERT INTO ScriptTest (sName, nID)
			VALUES ('new name', 5678)
"""

if tag:
	system.tag.write("[default]TestTag",1)
	inserted = system.db.runPrepUpdate(query, getKey=1)
	if inserted:
		system.tag.write("[default]TestTag",2)
	else:
		system.tag.write("[default]TestTag",3)

```

I am monitoring a Boolean Tag, if the current value is true, I want this script to trigger.

When I run this code on the Console it works just fine but not on the Client Event. Any insight in this matter? Perhaps I'm doing something wrong.

Thanks!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 10, 2025, 4:13pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/2 "2025-01-10T16:13:14Z")

</div>

If this is within the designer, make sure the preview mode is on (the 'play' button).

---

<div class="post-metadata">

**Author:** ![AdrielP](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@AdrielP](https://forum.inductiveautomation.com/u/AdrielP)\
**Post date:** [January 10, 2025, 4:27pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/3 "2025-01-10T16:27:40Z")

</div>

It is, I've also tested it within the vision project itself.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 10, 2025, 4:28pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/4 "2025-01-10T16:28:11Z")

</div>

Any error in the console?

---

<div class="post-metadata">

**Author:** ![AdrielP](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@AdrielP](https://forum.inductiveautomation.com/u/AdrielP)\
**Post date:** [January 10, 2025, 4:32pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/5 "2025-01-10T16:32:07Z")

</div>

None, in run mode and forcing the tag change to trigger the script, there are no errors. Same with the Log Viewer

---

<div class="post-metadata">

**Author:** ![stevenson](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stevenson](https://forum.inductiveautomation.com/u/stevenson)\
**Post date:** [January 10, 2025, 6:45pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/6 "2025-01-10T18:45:36Z")

</div>

go back to basics-  
you should be able to print into the console of a runtime client to troubleshoot.  
ctrl+shift+f7 in the client, trigger the bit from designer and the print statements should output to the console tab. if not there's something else happening.

for the record, client event script firing off a tag change means all open vision clients will perform their own insert.

---

<div class="post-metadata">

**Author:** ![AdrielP](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@AdrielP](https://forum.inductiveautomation.com/u/AdrielP)\
**Post date:** [January 10, 2025, 7:19pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/7 "2025-01-10T19:19:20Z")

</div>

The machine I'm working on has 4 vision clients (HMIs), the script will be monitoring a Bool tag that will prompt my script to collect all the tag data (value) from a structure (values containing dimensions and timestamps). These values, once stored into an array, will be used with the system.db.runPrepUpdate function to run a query that will insert all the values accordingly.

According to what you just mentioned, does this mean that this script will run 4 different times?

Also, looking at the logs I noticed that I am getting a new error, it says:  
"line 14, in browse\_nestedTags\_udts  
NameError: global name 'system' is not defined  
"  
I tried importing the system library and it still gives me the same error.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [January 10, 2025, 7:35pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/8 "2025-01-10T19:35:29Z")

</div>

> [@AdrielP](#):
>
> According to what you just mentioned, does this mean that this script will run 4 different times?

If the tags being monitored are global tags, then yes, the script will run once for each open client (in your case, 4 times).

The proper location for script this is a Gateway Tag Change Event.

> **[Gateway Event Scripts | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-in-ignition/gateway-event-scripts#tag-change-scripts)**
>
> Gateway Event Scripts Overview

Also, client events are not intended to trigger in the designer, and should be tested in actual clients.

> **[Client Event Scripts | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/ignition-modules/vision/scripting-in-vision/client-event-scripts)**
>
> Client Event Scripts Overview

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [January 10, 2025, 7:40pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/9 "2025-01-10T19:40:43Z")

</div>

Keep in mind that a Gateway Tag Change event runs in the Gateway scope, not client. Because of the scope difference you are required to provide the database connection name to `system.db.runPrepUpdate`.

> **[system.db.runPrepUpdate | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-db/system-db-runPrepUpdate#syntax---gateway)**
>
> Runs a prepared statement against the database, returning the number of rows that were affected.

There is also a page on the gateway where you can see a list of configured Gateway scripts and if they have executed, their execution times, and if they have an error.

> **[Systems | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/gateway/status/systems#gateway-scripts)**
>
> The Systems section of the Status tab displays information regarding the internal Gateway systems. This includes systems such as the currently running Transaction Groups as well as active Alarm Pipelines. These sections are not necessarily concerned...

---

<div class="post-metadata">

**Author:** ![AdrielP](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@AdrielP](https://forum.inductiveautomation.com/u/AdrielP)\
**Post date:** [January 10, 2025, 8:02pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/10 "2025-01-10T20:02:05Z")

</div>

Thank you Ryan, this has been great information. I was able to look into the Gateway scripts and found out that it's having an error running. Same error I posted above where it says "...'system' is not defined". Not sure why this is.

---

<div class="post-metadata">

**Author:** ![stevenson](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stevenson](https://forum.inductiveautomation.com/u/stevenson)\
**Post date:** [January 10, 2025, 8:03pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/11 "2025-01-10T20:03:41Z")

</div>

I'm guessing the snippet in OP was not the full script that's executing?  
What version of Ignition are you on? I've sometimes seen the error and either 1) adding import system to the front of the script or 2) a gateway restart usually resolves the issue

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [January 10, 2025, 8:05pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/12 "2025-01-10T20:05:55Z")

</div>

Also consider defining your script in your Project Library, and then calling it from the Gateway Tag Change script. This should also resolve the issue of having to import `system`.

> **[Project Library | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-in-ignition/project-library)**
>
> Scripts under the Project Library are a project-based resource that allows user created Python scripts to be configured. Objects and functions created in a Project Library script can be called from anywhere in the project. Project Library scripts are...

---

<div class="post-metadata">

**Author:** ![AdrielP](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@AdrielP](https://forum.inductiveautomation.com/u/AdrielP)\
**Post date:** [January 10, 2025, 8:10pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/13 "2025-01-10T20:10:01Z")

</div>

I read a previous forum that had the same issue, and I wrote "import system" at the top, before any code execution. Also, the code I'm trying to run is really big, it's mainly data formatting within defined functions and appending arrays. Its already tested and functional within the scripting console. The function that I'm interested in working is writing to the SQL. The error I'm receiving is specifically within one of my code blocks where I have a tag = system.tag.browse("parameter being passed in"). Thats where it's saying system not defined.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [January 10, 2025, 8:14pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/14 "2025-01-10T20:14:28Z")

</div>

> [@AdrielP](#):
>
> really big, it's mainly data formatting within defined functions and appending arrays.

Put all of this in your project library, break it out into re-usable functions where you can. You can start with the entire chunk as one whole function while you work on getting it to do what you want.

It also makes your event script definitions nice and clean, a single line calling the function from the project library.

---

<div class="post-metadata">

**Author:** ![AdrielP](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@AdrielP](https://forum.inductiveautomation.com/u/AdrielP)\
**Post date:** [January 10, 2025, 10:09pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/15 "2025-01-10T22:09:54Z")

</div>

> Also consider defining your script in your Project Library, and then calling it from the Gateway Tag Change script. This should also resolve the issue of having to import `system`.

This seemed to work! Thanks again Ryan. Breaking the code down into Functions and calling them from the Project Library did the trick. I was also importing in fnmatch and datetime, which were also conflicting with the Event script.

Thanks for the support guys 😄

---

<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:** [January 10, 2025, 11:27pm UTC](https://forum.inductiveautomation.com/t/client-event-script-tag-change/100733/16 "2025-01-10T23:27:30Z")

</div>

> [@ryan.white](#):
>
> Also consider defining your script in your Project Library, and then calling it from the Gateway Tag Change script. This should also resolve the issue of having to import `system`.

Note for posterity: This longstanding issue is fixed in 8.3.0, and this won't be necessary to get sane script scoping in client event scripts anymore. It's still generally a good idea to use the project library as much as possible, but not as critical.
