# NamedQuery in Script

**URL:** <https://forum.inductiveautomation.com/t/namedquery-in-script/58055>\
**Category:** Ignition\
**Created:** [April 5, 2022, 5:32pm UTC](https://forum.inductiveautomation.com/t/namedquery-in-script/58055 "2022-04-05T17:32:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Archer](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Andrew\_Archer](https://forum.inductiveautomation.com/u/Andrew_Archer)\
**Post date:** [April 5, 2022, 5:32pm UTC](https://forum.inductiveautomation.com/t/namedquery-in-script/58055/1 "2022-04-05T17:32:18Z")

</div>

Hi

I’m trying to run a namedquery from a script.  
If I don’t have parameters in the namedquery it works fine

def TestQueryDB():  
system.tag.writeToTag(“testtag”, system.db.runNamedQuery(“Downtime”,{}))

If I add a parameter to the named query, it works in the testing function using this  
string ‘2022-04-04 17:51:11’

but if I run it from script I get this error java.lang.ClassCastException: Cannot coerce value

def TestQueryDB():  
Yesterday = ‘2022-04-04 17:51:11’  
system.tag.writeToTag(“testtag”, system.db.runNamedQuery(“Downtime”,{Yesterday}))

I also tried it without the curly brackets around the parameterand it’s the same  
def TestQueryDB():  
Yesterday = ‘2022-04-04 17:51:11’  
system.tag.writeToTag(“testtag”, system.db.runNamedQuery(“Downtime”,Yesterday))

Could anyone tell me what I’m missing?  
Thanks

---

<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:** [April 5, 2022, 5:43pm UTC](https://forum.inductiveautomation.com/t/namedquery-in-script/58055/2 "2022-04-05T17:43:30Z")

</div>

What is the _type_ of the parameter in your query?  
You’re supplying a string, which is _not_ the same thing as an actual date/time.

You might just need to construct your date in a different way, or use `system.date.parse` to turn the string into an actual date object; consider exploring the `system.date` library functions:  
[https://docs.inductiveautomation.com/display/DOC81/system.date](https://docs.inductiveautomation.com/display/DOC81/system.date)

---

<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:** [April 5, 2022, 5:47pm UTC](https://forum.inductiveautomation.com/t/namedquery-in-script/58055/3 "2022-04-05T17:47:08Z")

</div>

Neither of your examples show you supplying a proper dictionary of parameters. Try this:

```auto
system.tag.writeToTag('testtag', system.db.runNamedQuery('Downtime', {'Yesterday': '2022-04-04 17:51:11'))

```

{ In the future, after pasting code here on the forum, highlight it and then click the “preformatted text” button. Failing to do that will treat your code as prose instead of code. Which, among other things, scrambles quotation marks so they cannot be cut/pasted. }

---

<div class="post-metadata">

**Author:** ![Andrew\_Archer](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Andrew\_Archer](https://forum.inductiveautomation.com/u/Andrew_Archer)\
**Post date:** [April 5, 2022, 5:54pm UTC](https://forum.inductiveautomation.com/t/namedquery-in-script/58055/4 "2022-04-05T17:54:56Z")

</div>

Ahh, that’s what I was missing, thanks a lot
