# Tag event script to write from UDT query tag to a UDT OPC tag

**URL:** <https://forum.inductiveautomation.com/t/tag-event-script-to-write-from-udt-query-tag-to-a-udt-opc-tag/80874>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [October 23, 2023, 7:01pm UTC](https://forum.inductiveautomation.com/t/tag-event-script-to-write-from-udt-query-tag-to-a-udt-opc-tag/80874 "2023-10-23T19:01:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Coughlin](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Steve\_Coughlin](https://forum.inductiveautomation.com/u/Steve_Coughlin)\
**Post date:** [October 23, 2023, 7:01pm UTC](https://forum.inductiveautomation.com/t/tag-event-script-to-write-from-udt-query-tag-to-a-udt-opc-tag/80874/1 "2023-10-23T19:01:06Z")

</div>

Hello,

I have a UDT query tag that on a value change I am hoping to write to a UDT OPC tag. However it doesn't seem to be working...

Here is the value changed tag event script I am running below:

```auto
path = "[default]Keyence Laser Marker MD-X2000/" + self.session.custom.IP_MachineID + "/" + self.session.custom.IP_MachineID + "_KeyenceLaserMarker/PartNumberOPC"

system.tag.writeBlocking([path],[currentValue.value])

```

I had this working before when I was not using a UDT 😕

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [October 23, 2023, 7:16pm UTC](https://forum.inductiveautomation.com/t/tag-event-script-to-write-from-udt-query-tag-to-a-udt-opc-tag/80874/2 "2023-10-23T19:16:23Z")

</div>

Have you confirmed your path and current value are correct before attempting to write to the tag? Namely those `MachineID` pieces of it. With these smaller scripts I've found it's pretty easy to mess around in the script console to identify where a weird issue might be hiding.

What do you mean it isn't working exactly -- the tag just isn't being written when you expect it to be?

---

<div class="post-metadata">

**Author:** ![leonardo.godoi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/leonardo.godoi/32/27178_2.png) [@leonardo.godoi](https://forum.inductiveautomation.com/u/leonardo.godoi)\
**Post date:** [October 23, 2023, 7:23pm UTC](https://forum.inductiveautomation.com/t/tag-event-script-to-write-from-udt-query-tag-to-a-udt-opc-tag/80874/3 "2023-10-23T19:23:33Z")

</div>

I'm not very experienced with Perspective but i believe the issue lies with the _ **`+ self.session.custom.IP_MachineID`** _ part of the tag change script, since it runs at the gateway scope, i don`t think a tag change script has acces to the self.session method, for example, if you had multiple sessions open, how would the tag script know which one to get the IP\_MachineID value from?

Under Status-\>Logs what erros are you getting when the value changes and the script fires?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/c/cc4a90f70226134885b065322123c603c7a04ac0.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 23, 2023, 7:40pm UTC](https://forum.inductiveautomation.com/t/tag-event-script-to-write-from-udt-query-tag-to-a-udt-opc-tag/80874/4 "2023-10-23T19:40:06Z")

</div>

What @leonardo.godoi said. Tag event do not have any access to variables that are part of a user interface session.

---

<div class="post-metadata">

**Author:** ![Steve\_Coughlin](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Steve\_Coughlin](https://forum.inductiveautomation.com/u/Steve_Coughlin)\
**Post date:** [October 23, 2023, 8:36pm UTC](https://forum.inductiveautomation.com/t/tag-event-script-to-write-from-udt-query-tag-to-a-udt-opc-tag/80874/5 "2023-10-23T20:36:45Z")

</div>

`com.inductiveautomation.ignition.common.script.JythonExecException: NameError: global name 'self' is not defined`

 ![Capture_ignition_lasermarker_troubleshooting](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/8/288dd1d75302c010eb7707c372c0051381c89263.png)

Looks like it's not happy with me using a variable from the user interface session like @pturmel mentioned.

How then would I be able to reference the machineID I am pulling from a different query based on the IP address of the device?

---

<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 23, 2023, 8:45pm UTC](https://forum.inductiveautomation.com/t/tag-event-script-to-write-from-udt-query-tag-to-a-udt-opc-tag/80874/6 "2023-10-23T20:45:23Z")

</div>

Do the query in gateway scope (timer script to memory tag, query tag, whatever) so the result is reachable by a tag script. If the UI is providing something global, bidirectionally bind that UI field to a memory tag.

---

<div class="post-metadata">

**Author:** ![Steve\_Coughlin](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Steve\_Coughlin](https://forum.inductiveautomation.com/u/Steve_Coughlin)\
**Post date:** [October 24, 2023, 1:48pm UTC](https://forum.inductiveautomation.com/t/tag-event-script-to-write-from-udt-query-tag-to-a-udt-opc-tag/80874/7 "2023-10-24T13:48:15Z")

</div>

I ended up just using a reference tag that is sourced from the query tag and then an event script on the reference tag that writes to an OPC tag. Thanks for the help!
