Calling named query from scripting

I get error

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Project name required for script execution: the Gateway Scripting Project is not set.

while calling system.db.runNamedQuery( "myUpdateQuery" , params) from a script.

From the documentation it appears we have to give “full path” of query name for a project scoped query or project name for a gateway scoped query

system.db.runNamedQuery( "ProjectName" , "folderName/myNamedQuery" , {})

What is full path of query? When I right click and “copy path” from the named query query I just get name of the query? Are there any restrictions on query names like no blanks , case sensitivity etc. How does it distinguish between the two scopes!

By where you call from.

Tag events (defined on the tag) are gateway scope, outside of any project. The gateway scripting project (which you apparently have not configured) would be a default for some cases.

Here is one way I learned on here on how to tell if you’re in a Vision scope

def isVision():
	Tells us if we are in the vision client or not.  Only works in client scope.
	import system.tag
	return system.tag.exists('[System]Client/User/Username')

You can use this to tell if you are in vision and and use the system.db..runNamedquery("myUpdatequery", params) version, and if not use the system.db.runNamedQuery("Project Name", "myUpdateQuery", params) version.

It is annoying to do it this way but unless a change is made to system.db.runNamedQuery’s function calls/param setup, there’s no way around it.

Ok got it. My script is from the alarm notification pipeline to call calculatedRoster function so obviously its in gateway scope! So I will pass the project name parameter and see if it works!

Nice way to check the scope. However we ought to know the scope ourselves based on where its called from. For me I thought its client scope but I realize now that its in gateway scope.

Agreed you should know what scope you are working in. Sometimes you want a script to be available in multiple scopes. I had a script that used system.db.runNamedQuery that was callable in both a vision scope and a gateway scope. So I needed to do

if env.isVision():
    system.db.runNamedQuery("someUpdateQuery", params)
    system.db.runNamedQuery("Some Project", "someUpdateQuery", params)

so that it would work in both situations.

Yes agreed. I didn’t think of this use case. Buts it’s an elegant solution! One can comeup with such solutions only with deep understanding of scripting in Ignition. Thanks it might be useful in some situations.

1 Like

Yesss! It worked like a champ! Thanks a lot!

I hardcoded the project name for quick testing. However how to get project name dynamically!? I guess some system tag will give it but would be nice to be spoon fed!

Not sure exactly what context you are working in but this may help

1 Like

Thanks a lot, I will change it to this function right away.

I have a follow up questions on this.

I made a named SQL query to use in my alarm pipeline. When I do work in the alarm pipeline I use the scripting Console to test code section by section then add it to the pipeline until it is completed.

My question is dealing with the following two aspect. When I was calling my named query from the Scription console. I could use:


But when I went into the pipeline I had to use:

system.db.runNamedQuery("ProjectName", "folderName/myNamedQuery")

Why does the scope change like that?

And if this is a normal aspect that can happen, will there be any update to the script console, say a tab for each scope to practice in? That would be really nice to have.

Pipelines run in the gateway. The designer is a separate process (not just separate, but different), even if run on the gateway machine. It simply cannot directly run something as gateway scope.

There are feature requests floating around to make some test environment that would hand a script from the designer to test run in the gateway, but that is a remarkably malware-ish behavior.

Consider making a gateway message handler that delegates to a project library script function you wish to test in gateway scope. Then it can be invoked from the designer scripting console with system.util.sendRequest.

1 Like

Tip: format code as code using the </> button, not quotations using the button (or > prefix). For more see Wiki - how to post code on this forum.