# How to insert record and return the identity value with the module sdk

**URL:** <https://forum.inductiveautomation.com/t/how-to-insert-record-and-return-the-identity-value-with-the-module-sdk/56723>\
**Category:** Module Development\
**Created:** [March 2, 2022, 3:24pm UTC](https://forum.inductiveautomation.com/t/how-to-insert-record-and-return-the-identity-value-with-the-module-sdk/56723 "2022-03-02T15:24:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![james.vosler](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@james.vosler](https://forum.inductiveautomation.com/u/james.vosler)\
**Post date:** [March 2, 2022, 3:24pm UTC](https://forum.inductiveautomation.com/t/how-to-insert-record-and-return-the-identity-value-with-the-module-sdk/56723/1 "2022-03-02T15:24:51Z")

</div>

How does one do the equivalent of system.db.runUpdateQuery or runPrepUpdate with getKey=1 within the sdk? According to the SRConnection docs [SRConnection](http://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.0/com/inductiveautomation/ignition/gateway/datasource/SRConnection.html), there is a runPrepInsertGetKey, but it returns:

> java.lang.NoSuchMethodError: 'int com.inductiveautomation.ignition.gateway.datasource.SRConnection.runPrepInsertGetKey(java.lang.String, java.lang.String, java.lang.String, java.lang.Object)'

This error occurred on 8.1.12 and 8.1.15

---

<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:** [March 2, 2022, 3:28pm UTC](https://forum.inductiveautomation.com/t/how-to-insert-record-and-return-the-identity-value-with-the-module-sdk/56723/2 "2022-03-02T15:28:55Z")

</div>

> [@james.vosler](#):
>
> java.lang.NoSuchMethodError: ‘int com.inductiveautomation.ignition.gateway.datasource.SRConnection.runPrepInsertGetKey(java.lang.String, java.lang.String, java.lang.String, java.lang.Object)’

That looks like you're calling an _instance_ method as a _static_ method. You need to obtain an SRConnection first, probably by going into a DatasourceManager off of the GatewayContext provided in your module's setup hook.

---

<div class="post-metadata">

**Author:** ![james.vosler](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@james.vosler](https://forum.inductiveautomation.com/u/james.vosler)\
**Post date:** [March 2, 2022, 3:54pm UTC](https://forum.inductiveautomation.com/t/how-to-insert-record-and-return-the-identity-value-with-the-module-sdk/56723/3 "2022-03-02T15:54:42Z")

</div>

It is being called as an instance method. The call works when calling 'runPrepUpdate', but returns the number of records created (1), not the Id.

> newKey = \_\_db.connection().runPrepInsertGetKey(query, ...

---

<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:** [March 2, 2022, 4:02pm UTC](https://forum.inductiveautomation.com/t/how-to-insert-record-and-return-the-identity-value-with-the-module-sdk/56723/4 "2022-03-02T16:02:50Z")

</div>

What language are you writing your module in? Can you make your typing explicit and/or see what you actually have after `__db.connection()`?

Unless you’re calling `com.inductiveautomation.ignition.gateway.datasource.DatasourceManager#getConnection(java.lang.String)`, I’m not sure you actually have an SRConnection.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [March 2, 2022, 4:03pm UTC](https://forum.inductiveautomation.com/t/how-to-insert-record-and-return-the-identity-value-with-the-module-sdk/56723/5 "2022-03-02T16:03:45Z")

</div>

`runPrepInsertGetKey` returns `long` starting with Ignition 8.1.6.

---

<div class="post-metadata">

**Author:** ![james.vosler](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@james.vosler](https://forum.inductiveautomation.com/u/james.vosler)\
**Post date:** [March 2, 2022, 4:47pm UTC](https://forum.inductiveautomation.com/t/how-to-insert-record-and-return-the-identity-value-with-the-module-sdk/56723/6 "2022-03-02T16:47:11Z")

</div>

That was it! When we started the modules the target gateway was back at 8.0.13, and the modules were built accordingly. This particular call was in a shared jar file which had upgraded references. Once all version references were updated and updated modules installed, the call to runPrepInsertGetKey runs successfully.

---

<div class="post-metadata">

**Author:** ![james.vosler](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@james.vosler](https://forum.inductiveautomation.com/u/james.vosler)\
**Post date:** [March 2, 2022, 4:48pm UTC](https://forum.inductiveautomation.com/t/how-to-insert-record-and-return-the-identity-value-with-the-module-sdk/56723/7 "2022-03-02T16:48:49Z")

</div>

Is this in fact the method used to implement the system.db.runPrepUpdate with getKey=1? runPrepInsertGetKey requires the table and column to be passed in, which is not part of the python interface.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [March 2, 2022, 4:58pm UTC](https://forum.inductiveautomation.com/t/how-to-insert-record-and-return-the-identity-value-with-the-module-sdk/56723/8 "2022-03-02T16:58:01Z")

</div>

`runUpdateQuery` and `runPrepUpdate` use it and pass in `null` values for table and column, despite what the Javadoc says about it…
