# Tag Event Scripts function must be declared before or after if currentValue.value:

**URL:** <https://forum.inductiveautomation.com/t/tag-event-scripts-function-must-be-declared-before-or-after-if-currentvalue-value/20152>\
**Category:** Ignition\
**Created:** [October 15, 2018, 2:38pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-function-must-be-declared-before-or-after-if-currentvalue-value/20152 "2018-10-15T14:38:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GeraGarcia](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/geragarcia/32/18471_2.png) [@GeraGarcia](https://forum.inductiveautomation.com/u/GeraGarcia)\
**Post date:** [October 15, 2018, 2:38pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-function-must-be-declared-before-or-after-if-currentvalue-value/20152/1 "2018-10-15T14:38:57Z")

</div>

which is the correct place for a declaration of the function in the Tag Event Scripts in Value Events-Value Changed before of the if currentValue.value: line (code1) or after the if currentValue.value: line (code2)

code1:

```auto
def funcion_hist(id_Variable):
	objTS = system.tag.read("[.]../HeartBeatSystem/ts_MsSQL")
	strConsultaSQL = system.db.runQuery("SELECT TOP(1) " + id_Variable + " FROM HrsOperadas 
                             ORDER BY hrsoperadas_ndx DESC", database="cnxMsSQL")
	for row in strConsultaSQL:
	strHrs = row[0]
	query = "INSERT INTO HistorialHrsOperadas (id_Variable, Valor, id_TimeStamp) VALUES (?, ?, ?)"
	args = [id_Variable.value, strHrs.value, objTS.value]
	system.db.runPrepUpdate(query, args, database="cnxMsSQL")
	
if currentValue.value:
	funcion_hist("BbaSubFlujo_1")
	funcion_hist("BbaSubFlujo_2")
	funcion_hist("BbaSubFlujo_3")
	funcion_hist("BbaSubFlujo_4")

```

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/9/9608e2c91bbdb8c56a3cb536f61040cce5c9c9b8.png)

or

code2:

```auto
if currentValue.value:
	def funcion_hist(id_Variable):
		objTS = system.tag.read("[.]../HeartBeatSystem/ts_MsSQL")
		strConsultaSQL = system.db.runQuery("SELECT TOP(1) " + id_Variable + " FROM HrsOperadas 
                             ORDER BY hrsoperadas_ndx DESC", database="cnxMsSQL")
		for row in strConsultaSQL:
		strHrs = row[0]
		query = "INSERT INTO HistorialHrsOperadas (id_Variable, Valor, id_TimeStamp) VALUES (?, ?, ?)"
		args = [id_Variable.value, strHrs.value, objTS.value]
		system.db.runPrepUpdate(query, args, database="cnxMsSQL")
			
	funcion_hist("BbaSubFlujo_1")
	funcion_hist("BbaSubFlujo_2")
	funcion_hist("BbaSubFlujo_3")
	funcion_hist("BbaSubFlujo_4")

```

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/e/e1ebec93d323a2018755eb0d98f8d518960fe352.png)

PD. sorry for the tab order but the original code have the correct tabs between lines.

Regards Gerardo

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [October 15, 2018, 2:52pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-function-must-be-declared-before-or-after-if-currentvalue-value/20152/2 "2018-10-15T14:52:03Z")

</div>

Hi Gerardo,

Please put your code in triple backquotes (```),or click on the “preformatted text” button.

![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/c/c1b093ec17f821fbdff950a8d47f849a6612f26c.png)

Then it will be formatted and keep spaces/tabs.

But from what I read, you wonder if you need to define the function inside or out of the if-clause. If you only use it inside the if-clause, it’s really a matter of taste.

It may give you a little performance gain to not create that function in the cases where currentValue is false. But usually, the caching in Jython is good enough that this won’t make a difference.

---

<div class="post-metadata">

**Author:** ![GeraGarcia](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/geragarcia/32/18471_2.png) [@GeraGarcia](https://forum.inductiveautomation.com/u/GeraGarcia)\
**Post date:** [October 15, 2018, 3:13pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-function-must-be-declared-before-or-after-if-currentvalue-value/20152/3 "2018-10-15T15:13:48Z")

</div>

Thanks Sanderd17

Regards

---

<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:** [October 15, 2018, 3:24pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-function-must-be-declared-before-or-after-if-currentvalue-value/20152/4 "2018-10-15T15:24:43Z")

</div>

> [@Sanderd17](#):
>
> But usually, the caching in Jython is good enough that this won't make a difference.

No, an inline function definition is executed every time, and cannot be cached. Caching and jython-java-jvm jit optimization cannot be applied to newly-defined functions. Put your function in a shared script if speed matters. Or even better, use a gateway tag change script and a project script module instead of the tag event.

---

<div class="post-metadata">

**Author:** ![GeraGarcia](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/geragarcia/32/18471_2.png) [@GeraGarcia](https://forum.inductiveautomation.com/u/GeraGarcia)\
**Post date:** [October 15, 2018, 3:36pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-function-must-be-declared-before-or-after-if-currentvalue-value/20152/5 "2018-10-15T15:36:50Z")

</div>

> [@pturmel](#):
>
> No, an inline function definition is executed every time, and cannot be cached. Caching and jython-java-jvm jit optimization cannot be applied to newly-defined functions. Put your function in a shared script if speed matters. Or even better, use a gateway tag change script and a project script module instead of the tag event.

hi pturmel.

This would be the correct way?

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/1/1e115843bc4c7d7e9f00721ad58fea2fafb2e707.png)

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/f/f75d10000ee0f6178d7a0e1c040fdf9deb8c9e01.png)

---

<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:** [October 15, 2018, 4:26pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-function-must-be-declared-before-or-after-if-currentvalue-value/20152/6 "2018-10-15T16:26:40Z")

</div>

> [@GeraGarcia](#):
>
> This would be the correct way?

Close. Remove the function definition from the script entirely. Move everything generic to a function in a project script module. Perform the objTS read once, in the event script, passing it in each function call. The function calls would look like `project.someModule.function_hist(objTS, "whatever")`.

---

<div class="post-metadata">

**Author:** ![GeraGarcia](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/geragarcia/32/18471_2.png) [@GeraGarcia](https://forum.inductiveautomation.com/u/GeraGarcia)\
**Post date:** [October 15, 2018, 8:10pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-function-must-be-declared-before-or-after-if-currentvalue-value/20152/7 "2018-10-15T20:10:57Z")

</div>

Thanks pturmel

Regards Gerardo.
