# Way to get the parameters associated with a named query?

**URL:** <https://forum.inductiveautomation.com/t/way-to-get-the-parameters-associated-with-a-named-query/60059>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [May 26, 2022, 12:42pm UTC](https://forum.inductiveautomation.com/t/way-to-get-the-parameters-associated-with-a-named-query/60059 "2022-05-26T12:42:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 26, 2022, 12:42pm UTC](https://forum.inductiveautomation.com/t/way-to-get-the-parameters-associated-with-a-named-query/60059/1 "2022-05-26T12:42:45Z")

</div>

When you bind to a named you get a list of the parameters to feed to the named query -  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/2/c2700882c592095b7c931bf48017fe73b4d9b449.png)

Is there a way to get this list programatically? I want to be able to check the dictionary I feed to my `system.db.runNamedQuery` against it before running it so I can log warnings for missing parameters - I’ve had typos in my dictionary keys before that caused an error that were very hard to run down.

I get why the `system.db.runNamedQuery` lets dictionary keys that don’t correlate with the parameters fall through - it’s great for my design, so I don’t have to `del dictionary['someKey']` to every other non-used key (that might be used for other logic) before running the query, but I do want to be able to at least catch the typos for myself in a log.

I want to modify my Form classses create function to be like

```auto
def __create(self):
    createNamedQuery = self.namedQueryFolder + '/create'
    self.logger.info("running create named query with payload")
    self.logger.info(str(self.newData))
    namedQueryParameters = someFunctionOn(createNamedQuery)
    # convert namedQueryParameters to dictionary
    for key in namedQueryParameters.keys():
        if key not in self.newData:
            self.logger.warn("%s in named query was not used"%(key))
    self.idx = system.db.runNamedQuery(createNamedQuery, self.newData, tx=self.tx, getKey=1)

```

I just don’t know what `someFunctionOn()` should be.

---

<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:** [May 26, 2022, 12:50pm UTC](https://forum.inductiveautomation.com/t/way-to-get-the-parameters-associated-with-a-named-query/60059/2 "2022-05-26T12:50:53Z")

</div>

A snippet from my BlobServe module:

```auto
            NamedQueryManager nqm = GatewayHook.getMyContext().getNamedQueryManager();
            NamedQuery nq = nqm.getQueryFromPath(project, namedQueryPath);
...
            List<Parameter> paramDefs = nq.getParameters();

```

Start here:

[https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.1/com/inductiveautomation/ignition/common/db/namedquery/package-summary.html](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.1/com/inductiveautomation/ignition/common/db/namedquery/package-summary.html)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [May 26, 2022, 12:51pm UTC](https://forum.inductiveautomation.com/t/way-to-get-the-parameters-associated-with-a-named-query/60059/3 "2022-05-26T12:51:05Z")

</div>

This probably isn’t the slickest way, but according to the [manual](https://docs.inductiveautomation.com/display/DOC81/Named+Queries) any version beyond 8.1.6, you can get this information from the resource.json file from

```auto
%installationDirectory%\data\projects\PROJECTNAME\ignition\named-query\QUERYNAME

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 26, 2022, 1:15pm UTC](https://forum.inductiveautomation.com/t/way-to-get-the-parameters-associated-with-a-named-query/60059/4 "2022-05-26T13:15:57Z")

</div>

I am having trouble getting the `GatewayHook` part. I am lost in the weeds with the documentation -  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/a/4aadb232c0fbd7dbad2cb7192bd549aa7964e936.png)

Looking at your first line I searched for getNamedQueryManager and see this, and given you use a getMyContext, I highlighted what I think is the right one -

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/0/b0b2565ee873b616428aa224bd9d172be7d2b6c1.png)

Doing `import com.inductiveautomation.ignition.gateway.model.GatewayContext` throws me an import error that there is no module named model.

I know this is a lapse in my knowledge with reading java documentaiton/java knowledge in general like interfaces etc. How can I get the GatewayHook?

---

<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:** [May 26, 2022, 1:50pm UTC](https://forum.inductiveautomation.com/t/way-to-get-the-parameters-associated-with-a-named-query/60059/5 "2022-05-26T13:50:43Z")

</div>

Sorry, I assumed too much. The key is to get the `GatewayContext`. Outside of a module, the typical (but unsupported) method is to import `IgnitionGateway` then use `.get()` to obtain the platform singleton.

{ This stuff only works in gateway scope. Delegate to gateway scope via a message handler when in designer or vision client scopes. }

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 26, 2022, 2:02pm UTC](https://forum.inductiveautomation.com/t/way-to-get-the-parameters-associated-with-a-named-query/60059/6 "2022-05-26T14:02:35Z")

</div>

> [@pturmel](#):
>
> { This stuff only works in gateway scope. Delegate to gateway scope via a message handler when in designer or vision client scopes. }

Ah just realized that when trying. You think it would be problematic to be calling and using `IgntionGateway` to get these named query parameters somewhat frequently? Or would it be safe? My second thought is to write a script to run on the gateway context that goes through all my named queries, saves the path and their parameters into a database table in case it's problematic.

---

<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:** [May 26, 2022, 2:08pm UTC](https://forum.inductiveautomation.com/t/way-to-get-the-parameters-associated-with-a-named-query/60059/7 "2022-05-26T14:08:49Z")

</div>

> [@bkarabinchak.psi](#):
>
> get these named query parameters somewhat frequently?

I wouldn't be troubled. These are the interfaces that run the Queries themselves, so I would expect them to be well-optimized. If you are concerned, at most I would cache in a script module top level dictionary. That would be wiped when the project is saved.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 26, 2022, 2:35pm UTC](https://forum.inductiveautomation.com/t/way-to-get-the-parameters-associated-with-a-named-query/60059/8 "2022-05-26T14:35:33Z")

</div>

This is where I ended up with, which I call with a `system.util.sendRequest`

```auto
def getNamedQueryParameters(payload):
	from com.inductiveautomation.ignition.gateway import IgnitionGateway
	project = payload['project']
	query = payload['query']
	namedQuery = IgnitionGateway.get().getNamedQueryManager().getQueryFromPath(project, query)
	parameters = namedQuery.getParameters()
	listOfParams = [param.getIdentifier() for param in parameters]
	logger.info(str(listOfParams))
	return listOfParams

```

And this logs exaclty what I need so I can utlize it.

I am testing it in script console now though and I actually am not getting the result - is that a known thing with testing sendRequest in script console?

```auto
project = 'PSM4_Project'
messageHandler = 'getNamedQueryParameters'
d = {'project':project, 'query':'forms/Customer/get'}
result = system.util.sendRequest(project, messageHandler, d)
print str(result)
print str(type(result))

```

Gives me None/NoneType, but I would expect the list of params that are logged. I assume it will work in the client, I just like to try to build these things in script console as I find it easier to debug and fix issues.

EDIT: Issue with sendRequest was of course my own mistake. I had

```auto
def handleMessage(payload):
	classes.helpers.getNamedQueryParameters(payload)

```

but needed

```auto
def handleMessage(payload):
	return classes.helpers.getNamedQueryParameters(payload)

```
