# Best methods for checking script scope

**URL:** <https://forum.inductiveautomation.com/t/best-methods-for-checking-script-scope/30201>\
**Category:** Ignition\
**Created:** [November 24, 2019, 10:32pm UTC](https://forum.inductiveautomation.com/t/best-methods-for-checking-script-scope/30201 "2019-11-24T22:32:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [November 24, 2019, 10:32pm UTC](https://forum.inductiveautomation.com/t/best-methods-for-checking-script-scope/30201/1 "2019-11-24T22:32:20Z")

</div>

I am trying to call system.db.runNamedQuery() in a Shared script. I would like to be able to call this from both Project scope (button on a client) as well as Gateway scope (tag change script based on datetime trigger Sunday 11PM).

Similar problem to:

> [@Named query scope](http://forum.inductiveautomation.com/t/named-query-scope/25928/8):
>
> Ok this makes sense. would there be any changes needed to add inheritance to a project that currently holds the scripts? Im coming from an upgraded 7.9 structure so the stack would look like “Global --\> project1 --\> project2” where project1 has the named query and project2 needs to run the query. currently both project1 and project2 have global as a parent. Project1 has no users so it wouldn’t have to run as a client ever but it holds all original tag change scripts and transaction groups.

> [@"Cannot coerce value" Named Query Path "into type: interface java.util.Map](http://forum.inductiveautomation.com/t/cannot-coerce-value-named-query-path-into-type-interface-java-util-map/18042):
>
> I have a script designed to build a list of parameters and then execute a named query. The script is a shared (Gateway) level script so the format of my system.db.runNamedQuery instruction is system.db.runNamedQuery(‘project name’, ‘named query path/name’, parameters). The error message I am getting is: java.lang.ClassCastException: java.lang.ClassCastException: Cannot coerce value ‘TnT Queries/namedQ\_InsertNewMCID’ into type: interface java.util.Map The ‘TnT Queries/namedQ\_InsertnewMCID’ name…

Is there a reliable way to check scoping so that i can do something like:

```auto
If scope == Gateway:
   result = system.db.runNamedQuery('projectName','queryName',args)
else:
   result = system.db.runNamedQuery('queryName',args)

```

A lot of my scripts written in my shared libraries are able to be run from either scope, this is the first time i've run into this problem.

---

<div class="post-metadata">

**Author:** ![nicholas](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@nicholas](https://forum.inductiveautomation.com/u/nicholas)\
**Post date:** [November 25, 2019, 4:29pm UTC](https://forum.inductiveautomation.com/t/best-methods-for-checking-script-scope/30201/2 "2019-11-25T16:29:54Z")

</div>

Try this:

```auto
	def getScope():
		"""
		Returns:
			scope (int): 0=Gateway, 1=Client, 2=Designer
		"""
		try:
			if system.util.getSystemFlags() & system.util.CLIENT_FLAG:
				location = 1
			elif system.util.getSystemFlags() & system.util.DESIGNER_FLAG:
				location = 2
		except AttributeError:
			# system.util.getSystemFlags() does not exist in gateway scope
			location = 0
		return location

```

(Source: [Which scope is my code running in?](http://forum.inductiveautomation.com/t/which-scope-is-my-code-running-in/15540/3?u=nicholas))

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [November 25, 2019, 8:24pm UTC](https://forum.inductiveautomation.com/t/best-methods-for-checking-script-scope/30201/3 "2019-11-25T20:24:00Z")

</div>

Thanks Nick,

I have ended up creating a function using the `ApplicationScope.getGlobalScope()` method which seems to work fine in all scopes.

---

<div class="post-metadata">

**Author:** ![nicholas](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@nicholas](https://forum.inductiveautomation.com/u/nicholas)\
**Post date:** [November 25, 2019, 8:47pm UTC](https://forum.inductiveautomation.com/t/best-methods-for-checking-script-scope/30201/4 "2019-11-25T20:47:26Z")

</div>

Yes, that method is probably supported better, good call.
