# Gateway event script sometimes fails to execute a function

**URL:** <https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539>\
**Category:** Ignition\
**Created:** [July 9, 2020, 1:36pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539 "2020-07-09T13:36:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [July 9, 2020, 1:36pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/1 "2020-07-09T13:36:04Z")

</div>

I have a tag change gateway event script running.

```
if not initialChange:
    var_Furnace = '1W'
    tempReached = system.tag.read('somePath').value
    tempReachedAck = system.tag.read('somePath').value

    if tempReachedAck == 1:
        pass
    else:
        pairID = system.tag.read('somePath).value
        id1 = system.tag.read('somePath').value
        id2 = system.tag.read('somePath').value

        if pairID > 0:
            #THIS LINE SOMETIMES RUNS
            project.functions.tempReached(var_Furnace, id1, id2) 

            #THIS LINE ALWAYS RUNS
            system.tag.write('somePath', 1)

```

Sometimes the second the second to last line of code doesn’t run though (it appears to not run). I am doing a few database queries and updates but nothing too intensive. I use an info logger but nothing appears on my console log.

```
furnaceLogger = system.util.getLogger("Furnaces")

```

Is there a way to log some steps taking place during that function call? What should I check in my gateway to see if that function is actually executing?

---

<div class="post-metadata">

**Author:** ![KGarner](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgarner/32/15999_2.png) [@KGarner](https://forum.inductiveautomation.com/u/KGarner)\
**Post date:** [July 9, 2020, 1:43pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/2 "2020-07-09T13:43:10Z")

</div>

I don’t have a complete answer based on the code as I have never used Gateway Event Tag Change Scripts, but I do know that the furnaceLogger.info() will log to the Gateway Status page rather than the console log.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [July 9, 2020, 1:54pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/3 "2020-07-09T13:54:51Z")

</div>

Ok, I see.

I’ve looked at the logs around the time I ran the last test and the function was indeed “skipped” from what I can tell. There are several furnaces. Furnace #1 ran correctly, furnace #2 skipped the function, and furnace #3 ran correctly. I know the function was “skipped” because in the function being called the first thing I do is log that I am accessing that function.

```
furnaceLogger.info('Executing function project.functions.tempReached(var_furnace, id1, id2)')

```

I then triggered the same tag manually after #1 and #3 ran and the logs show #2 correctly running.

Thoughts?

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [July 9, 2020, 2:06pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/4 "2020-07-09T14:06:08Z")

</div>

I’ve added a return value on the function being called and write that info to a memory tag. I will try to replicate the issue again as I am currently at the customer site.

---

<div class="post-metadata">

**Author:** ![KGarner](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgarner/32/15999_2.png) [@KGarner](https://forum.inductiveautomation.com/u/KGarner)\
**Post date:** [July 9, 2020, 2:23pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/5 "2020-07-09T14:23:37Z")

</div>

I would be curious to know which project hosts the project.functions.tempReached function and also what Project the Gateway uses for its scripts

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [July 9, 2020, 2:48pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/6 "2020-07-09T14:48:29Z")

</div>

> [@KGarner](#):
>
> what Project the Gateway uses for its scripts

I don't understand what you mean. How can I look this info up?

---

<div class="post-metadata">

**Author:** ![KGarner](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgarner/32/15999_2.png) [@KGarner](https://forum.inductiveautomation.com/u/KGarner)\
**Post date:** [July 9, 2020, 3:21pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/7 "2020-07-09T15:21:51Z")

</div>

[https://docs.inductiveautomation.com/display/DOC80/Project+Library#ProjectLibrary-GatewayScriptingProject](https://docs.inductiveautomation.com/display/DOC80/Project+Library#ProjectLibrary-GatewayScriptingProject)

The Gateway Settings has a field for that. I’m assuming you’re using 8.0

If you’re using 7.9, I’m not exactly sure why that function wouldn’t be called

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [July 9, 2020, 5:23pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/8 "2020-07-09T17:23:38Z")

</div>

I don’t see how your project script would run. With how your code looks I’m assuming your not using 8.0. On 7.9 a project script runs for a specific project but you stated you are running this on a gateway script. A gateway script can not call a project script because it is running on the gateway and is not linked to a specific project. It would need to be a global shared script. You would need to move the script to your global shared scripts and then change your path to call the script to reflect it.

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [July 9, 2020, 5:26pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/9 "2020-07-09T17:26:27Z")

</div>

> [@bmeyers](#):
>
> I have a tag change gateway event script running.

@bpreston OP stated a tag change gateway event script which means it will have access to the project script library.

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [July 9, 2020, 5:57pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/10 "2020-07-09T17:57:58Z")

</div>

My mistake, I was grouping all the gateway stuff into one in my head.

Even though it may be able to call a project script, how are the queries set up since he said its running queries. Some places you don’t call out the project name when calling a query, others you do. Calling it from a gateway timer script it does have to be called but he should also see an error in his logs if the script is failing unless he did something in the project script to prevent it.

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [July 9, 2020, 6:02pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/11 "2020-07-09T18:02:58Z")

</div>

A gateway event script -\> timer does not require the project name either. Those scripts are project based so the project name is not required.

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [July 9, 2020, 6:09pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/12 "2020-07-09T18:09:22Z")

</div>

You would think that but I tested it before I replied because I knew I was bit by it before. If I do a simple project query that just calls a named query and leave the project out of the runNamedQuery function it works fine calling it from the script console but the gateway timer script will fail on the line that calls the project script. If I add in the project name then it will fail in the script console but the gateway timer script will run.

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [July 9, 2020, 6:10pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/13 "2020-07-09T18:10:07Z")

</div>

Yes runNamedQuery will require the project name, but runPrepQuery doesn’t.  
This makes sense since runPrepQuery is a talking directly to a database where the named query is a project resource. So at the gateway level the project name makes sense here.

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [July 9, 2020, 6:11pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/14 "2020-07-09T18:11:05Z")

</div>

Have to love consistency. Personally I would prefer it if I could just put in the project name everywhere and have it run everywhere. There would be less chances of accidentally doing it the wrong way for where you call it.

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [July 9, 2020, 6:15pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/15 "2020-07-09T18:15:32Z")

</div>

Its the reason a lot of my scripts have something like this in them.

```auto
def queryScript(tBit=0):
	queryPath = "query"
	params = {}
	if tBit:
		data = system.db.runNamedQuery(queryPath,params)
	else:
		data = system.db.runNamedQuery("projectName",queryPath,params)

```

Then I can call it without anything passed across in places I need the project name passed in and I pass a 1 in when I call the script if I don’t need the project name.

---

<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:** [July 9, 2020, 6:41pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/16 "2020-07-09T18:41:29Z")

</div>

`runNamedQuery()`'s function signature was mis-designed, and in a way that prevents it from being fixed in any backward-compatible form. The ‘project’ argument should have been _ **after** _ path and parameters, and optional in project contexts. I’d love to see it deprecated, with the replacement as above. Shortened to `.namedQuery()`, perhaps. (I also think clients should be able to call Named Queries in other projects. Not currently possible, though it can be worked around with sendRequest.)

---

<div class="post-metadata">

**Author:** ![bmusson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmusson/32/87530_2.png) [@bmusson](https://forum.inductiveautomation.com/u/bmusson)\
**Post date:** [July 9, 2020, 11:48pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/17 "2020-07-09T23:48:02Z")

</div>

> [@pturmel](#):
>
> The ‘project’ argument should have been _ **after** _ path and parameters, and optional in project contexts. I’d love to see it deprecated, with the replacement as above.

I’ve got to agree with this. From a users perspective, runNamedQuery() is unnecessarily quirky.

> [@beginNamedQueryTransaction requires undocumented 'project' argument in gateway scope](http://forum.inductiveautomation.com/t/beginnamedquerytransaction-requires-undocumented-project-argument-in-gateway-scope/34135/3):
>
> I think this is the source of some confusion. In the gateway, you know the project when the caller is a gateway event script. You don’t know the project when called from a tag event. The corresponding scripting functions in system.db.\* that have a datasource argument should have been the model for the named query functions. These functions take an optional datasource in client and gateway project scopes, but the argument is mandatory in gateway non-project scope. Arguably, with v8’s new gl…

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [July 10, 2020, 12:38pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/18 "2020-07-10T12:38:54Z")

</div>

Someone has posted this in ideas for anyone interested in seeing this modified.  
[https://inductiveautomation.canny.io/ignition-features-and-ideas/p/simplify-systemdbrunnamedquery-gatewayproject-scope](https://inductiveautomation.canny.io/ignition-features-and-ideas/p/simplify-systemdbrunnamedquery-gatewayproject-scope)

---

<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:** [July 10, 2020, 1:02pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/19 "2020-07-10T13:02:14Z")

</div>

Unfortunately, I still can’t log into the ideas site. It’s been quite a while since I was notified that ticket was in progress… /:

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [July 10, 2020, 3:11pm UTC](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539/20 "2020-07-10T15:11:03Z")

</div>

This is not true from what I’ve seen this morning. I’m still investigating the first issue I had with a gateway script but on a separate tag change gateway script the code can most definitely not call a project script.

[Next page](https://forum.inductiveautomation.com/t/gateway-event-script-sometimes-fails-to-execute-a-function/36539.md?page=2)
