# system.db.runNamedQuery to execute between scripting

**URL:** <https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [June 2, 2021, 3:36am UTC](https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334 "2021-06-02T03:36:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nelson.hurtado](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nelson.hurtado/32/18950_2.png) [@nelson.hurtado](https://forum.inductiveautomation.com/u/nelson.hurtado)\
**Post date:** [June 2, 2021, 3:36am UTC](https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334/1 "2021-06-02T03:36:55Z")

</div>

I have the next Script that I want to execute inside a template to animate the background color:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/d/5d38fc800d8bd457c74a748d6c383a57b29cb747.png)  
When I execute as Expression: runScript(“Dropdown”), I have the following message issue:  
Cannot coerce value ‘\<module Dropdown at 2\>’ into type: class java.awt.Color  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/7/d7dc51a774aac45d59b489bab55ebddcdcea7f2c.png)  
but if I execute each SQL query as Named Query it works. I don’t know How can I fix this issue, Any idea or suggestions, please?

---

<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:** [June 2, 2021, 11:14am UTC](https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334/2 "2021-06-02T11:14:46Z")

</div>

You have your code in the top level of a script module named “Dropdown” ? That won’t work. You must define a **function** with your code in a script module, and call the function.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [June 2, 2021, 12:29pm UTC](https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334/3 "2021-06-02T12:29:30Z")

</div>

Vision is programming is events based. Components (and components inside of templates, as well as templates themselves once you instantiate them) have a bunch of different event listeners/extension functions. You need to choose which event triggers your script. Do you want it when some property on the component changes? When the mouse is clicked, or when the mouse is released? When something is double clicked? Etc.

Do what was suggested, put this code inside a function, and then decide where the appropriate time to call that function is, which extension function? A simple example for running the function every time a new selection is made on the dropdown would be to put this under the property change script of the component -

```auto
from myModule import myFunction

if event.propertyName == 'selectedValue':
    myFunction(event.source)

```

`event.source` would pass a reference to the dropdown to your function so you could look at the dropdown data and manipulate background color.

Just a side note though, the expression language is pretty powerful, and it looks like your just basing your background color on two named queries. Why not add two custom properties on your dropdown, one for each query result, and then using those values, bind your background color property on an expression that uses those values?

---

<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:** [June 2, 2021, 12:52pm UTC](https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334/4 "2021-06-02T12:52:48Z")

</div>

> [@bkarabinchak.psi](#):
>
> `from myModule import myFunction`

+1 for everything @bkarabinchak.psi said, except that. Don't import from other script modules. You can have code lifetime problems. Always use fully qualified names from script modules. `myModule.myFunction(event.source)` in the posted example.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [June 2, 2021, 1:00pm UTC](https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334/5 "2021-06-02T13:00:56Z")

</div>

@pturmel  
You’re saying just to `import myModule` and then call the fully qualified name? Or is `import myModule` problematic itself?

---

<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:** [June 2, 2021, 1:23pm UTC](https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334/6 "2021-06-02T13:23:23Z")

</div>

`import myModule` is unnecessary, as other script module names are implicitly imported. `from myModule import x` is trouble. There have been problems where `x` gets stuck on old code after edits to myModule. If you feel you must do this, for readability’s sake, do it within your functions, not at the top level.

---

<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:** [June 2, 2021, 3:48pm UTC](https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334/8 "2021-06-02T15:48:21Z")

</div>

You’ll have to show more. Your script module code. Your complete runscript expression. A view of your template’s component tree.

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [June 2, 2021, 4:48pm UTC](https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334/10 "2021-06-02T16:48:44Z")

</div>

This implementation looks familiar to me. This is non-ethical attitude @nelson.hurtado, just do it by your own.

---

<div class="post-metadata">

**Author:** ![nelson.hurtado](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nelson.hurtado/32/18950_2.png) [@nelson.hurtado](https://forum.inductiveautomation.com/u/nelson.hurtado)\
**Post date:** [June 2, 2021, 4:56pm UTC](https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334/11 "2021-06-02T16:56:43Z")

</div>

I’ve worked for two days for my own and it doesn’t work, I need some help no more than a clue to reach the solution!

---

<div class="post-metadata">

**Author:** ![Paolo.Mariani](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paolo.mariani/32/3285_2.png) [@Paolo.Mariani](https://forum.inductiveautomation.com/u/Paolo.Mariani)\
**Post date:** [June 2, 2021, 5:06pm UTC](https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334/13 "2021-06-02T17:06:59Z")

</div>

Closed on June 2, 2021, 5:06pm UTC



---

<div class="post-metadata">

**Author:** ![Paolo.Mariani](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paolo.mariani/32/3285_2.png) [@Paolo.Mariani](https://forum.inductiveautomation.com/u/Paolo.Mariani)\
**Post date:** [June 2, 2021, 5:10pm UTC](https://forum.inductiveautomation.com/t/system-db-runnamedquery-to-execute-between-scripting/47334/14 "2021-06-02T17:10:52Z")

</div>

@jespinmartin1 is correct @nelson.hurtado. This is a question from the test, and as such, if you need help on the test, you need to email in to [training@inductiveautomation.com](mailto:training@inductiveautomation.com) for help. Help from other users on the forum is not allowed, as the test is meant to be a test of your efforts and not a group project.
