# Perspective audit log query to database

**URL:** <https://forum.inductiveautomation.com/t/perspective-audit-log-query-to-database/73900>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [May 18, 2023, 2:33pm UTC](https://forum.inductiveautomation.com/t/perspective-audit-log-query-to-database/73900 "2023-05-18T14:33:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ladrone](https://avatars.discourse-cdn.com/v4/letter/l/a698b9/32.png) [@Ladrone](https://forum.inductiveautomation.com/u/Ladrone)\
**Post date:** [May 18, 2023, 2:33pm UTC](https://forum.inductiveautomation.com/t/perspective-audit-log-query-to-database/73900/1 "2023-05-18T14:33:07Z")

</div>

In perspective at the moment there isn't the possibility to log in the audit the query launched aginst a database.  
I've used all named query in my project: in your opinion what is the best approach to log them in the audit using `system.util.audit`?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 18, 2023, 3:22pm UTC](https://forum.inductiveautomation.com/t/perspective-audit-log-query-to-database/73900/2 "2023-05-18T15:22:32Z")

</div>

Introduce a project library function that "wraps" `system.db.runNamedQuery` and automatically performs `system.util.audit` at the same time. Refactor your existing calls to run that script.

---

<div class="post-metadata">

**Author:** ![Ladrone](https://avatars.discourse-cdn.com/v4/letter/l/a698b9/32.png) [@Ladrone](https://forum.inductiveautomation.com/u/Ladrone)\
**Post date:** [May 19, 2023, 9:49am UTC](https://forum.inductiveautomation.com/t/perspective-audit-log-query-to-database/73900/3 "2023-05-19T09:49:41Z")

</div>

I configured mi function as follow

```python
def runNamedQueryAudit(path, parameters=None, tx='', getKey=False):
	ret = system.db.runNamedQuery(path, parameters, tx, getKey)
	action = 'query'
	action_target = path + ' - parameters: ' + str(parameters) if parameters is not None else path
	action_value = str(ret)
	system.util.audit(action=action, actionTarget=action_target, actionValue=action_value)
	
	return ret

```

but I when I call it from outside with

```python
connectionInfo = system.db.getConnectionInfo()
dbName = connectionInfo.getValueAt(0, 'name')
projName = system.util.getProjectName()
txId = system.db.beginNamedQueryTransaction(projName, dbName)
par = {
	'masterRecipeId': 10
}
test.db.runNamedQueryAudit('mr_duplicate/duplicateMrHeader', parameters=par, tx=txId)
system.db.commitTransaction(txId)
system.db.closeTransaction(txId)

```

have issue at line `ret = system.db.runNamedQuery(path, parameters, tx, getKey)` of the library, it gave me this error:

```plaintext
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
INFO | jvm 2 | 2023/05/19 11:37:15 | File "<function:runAction>", line 16, in runAction
INFO | jvm 2 | 2023/05/19 11:37:15 | File "<module:test.db>", line 8, in runNamedQueryAudit
INFO | jvm 2 | 2023/05/19 11:37:15 | at com.inductiveautomation.ignition.common.TypeUtilities.coerce(TypeUtilities.java:1527)
INFO | jvm 2 | 2023/05/19 11:37:15 | at com.inductiveautomation.ignition.common.script.builtin.PyArgumentMap.coerce(PyArgumentMap.java:130)
INFO | jvm 2 | 2023/05/19 11:37:15 | at com.inductiveautomation.ignition.common.script.builtin.PyArgumentMap.interpretPyArgs(PyArgumentMap.java:82)
INFO | jvm 2 | 2023/05/19 11:37:15 | at com.inductiveautomation.ignition.common.script.builtin.PyArgumentMap.interpretPyArgs(PyArgumentMap.java:40)
INFO | jvm 2 | 2023/05/19 11:37:15 | at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities.interpretWithOptionalProject(GatewayDBUtilities.java:467)
INFO | jvm 2 | 2023/05/19 11:37:15 | at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities.runNamedQuery(GatewayDBUtilities.java:391)
INFO | jvm 2 | 2023/05/19 11:37:15 | at jdk.internal.reflect.GeneratedMethodAccessor23.invoke(Unknown Source)
INFO | jvm 2 | 2023/05/19 11:37:15 | at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
INFO | jvm 2 | 2023/05/19 11:37:15 | at java.base/java.lang.reflect.Method.invoke(Unknown Source)
INFO | jvm 2 | 2023/05/19 11:37:15 | java.lang.ClassCastException: java.lang.ClassCastException: Cannot coerce value '0aa6c53d-451a-4b6a-aa4b-4098752dd04d' into type: interface java.util.Map

```

As far as I understand it fail converting to map the `parameters` argument of my function.  
If in my call I use the normal `system.db.runNamedQuery` function everyting work fine!

Any idea on how to fix it?

---

<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 19, 2023, 12:09pm UTC](https://forum.inductiveautomation.com/t/perspective-audit-log-query-to-database/73900/4 "2023-05-19T12:09:14Z")

</div>

Some system functions need additional arguments when called from gateway scope. Running a named query is one of them.

Testing in the script console uses designer scope, which is a variant of Vision Client scope. You are calling from Gateway scope or Perspective scope (a variant of Gateway scope). Check the docs!

Project library scripts **do not have their own scope**. They run in whatever scope calls them.

(Testing in the script console is never appropriate for gateway or perspective events.)

---

<div class="post-metadata">

**Author:** ![Ladrone](https://avatars.discourse-cdn.com/v4/letter/l/a698b9/32.png) [@Ladrone](https://forum.inductiveautomation.com/u/Ladrone)\
**Post date:** [May 19, 2023, 12:35pm UTC](https://forum.inductiveautomation.com/t/perspective-audit-log-query-to-database/73900/5 "2023-05-19T12:35:47Z")

</div>

> [@pturmel](#):
>
> Project library scripts **do not have their own scope**. They run in whatever scope calls them.

In the meantime I was figouring it out, the solution I found was in this post

> [@Best methods for checking script scope](https://forum.inductiveautomation.com/t/best-methods-for-checking-script-scope/30201/2):
>
> Try this: 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))

> [@pturmel](#):
>
> (Testing in the script console is never appropriate for gateway or perspective events.)

I wasn't testing it from console, but calling it from a button in persepective! In the console give me some other absurd errors by the way!
