NamedQuery in Script

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

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

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

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. }

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