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)
else:
    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

https://docs.inductiveautomation.com/display/DOC80/system.util.getProjectName

1 Like

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