# Add instance of UDT from a tag valueChanged event script in another UDT

**URL:** <https://forum.inductiveautomation.com/t/add-instance-of-udt-from-a-tag-valuechanged-event-script-in-another-udt/14840>\
**Category:** Ignition\
**Created:** [July 20, 2017, 5:08am UTC](https://forum.inductiveautomation.com/t/add-instance-of-udt-from-a-tag-valuechanged-event-script-in-another-udt/14840 "2017-07-20T05:08:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsorlie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jsorlie/32/3645_2.png) [@jsorlie](https://forum.inductiveautomation.com/u/jsorlie)\
**Post date:** [July 20, 2017, 5:08am UTC](https://forum.inductiveautomation.com/t/add-instance-of-udt-from-a-tag-valuechanged-event-script-in-another-udt/14840/1 "2017-07-20T05:08:03Z")

</div>

I am having trouble with `system.tag.addTag()`. Context is adding instances of a UDT. Execution of the `system.tag.addTag()` results in a java exception:

> system.tag.addTag([Default]site/area1/lineX): Exception type: , value: java.lang.IllegalArgumentException: UDT [Default]_types_/myUDTs/line does not exist. Can not create instance.

No combination of tinkering with tag provider specifications seems to work.

* * *

**Back story:** We have two UDT's defined:

- `myUDTs/index`
- `myUDTs/line`

The first is supposed to serve as an index of "lines" that constitute an "area". For sake of illustration I have distilled the **`index`** UDT down to a single memory tag: (of type String instead of say a dataset).

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

An instance of the **`index`** UDT looks like this:

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

When the value of the string is changed, the `valueChanged` event script should create an instance of the **`line`** UDT in the same folder where the index is located, eg. `area1` in the example shown above.

The **`line`** UDT here is also distilled down for illustrative purposes:

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

To facilitate your gaming pleasure, I packaged the `myUDTs` folder and the instance folder in two separate XML payloads:

* * *

[tags-myUDTs.xml](https://us1.discourse-cdn.com/inductiveautomation/original/2X/1/14361ccd42441204bf5935ebb85507ca2907d5c7.xml) (3.3 KB)

[tags-site.xml](https://us1.discourse-cdn.com/inductiveautomation/original/2X/9/99235955016d8a27d6d5a178b91ec181130332af.xml) (479 Bytes)

* * *

I have tried packaging the ` system.tag.addTag()` call up inside both project and shared python libraries. As this simplified toy example serves to demonstrate, none of that complexity is causing the Java exception.

I wonder if anyone can enlighten me on this issue. I found a few related posts in the forum but no answers. As I mentioned above, I have experimented with all possible ways of specifying the tag provider. It seems `system.tag.addTag()` cannot find the UDT definitions when the program code is bundled up inside a UDT definition.

> [@Event Script an system.tag.addTag](http://forum.inductiveautomation.com/t/event-script-an-system-tag-addtag/10923):
>
> Hello, I’m wondering if it’s possible to create a tag Event script with system.tag.addTag() ? I configured alarm tag with PyDictionary alarmConfig like this alarmConfig={"Alarm ":[[“name”, “Value”, "Test "], [“setpointA”,“Value”, 1], [“displayPath”,“Tag”,MessPath]]}) but I didn’t find anything for the tag event Regards

Do I need to wield the SDK to make this work?

> [@Instanciate UDT tags with the SDK](http://forum.inductiveautomation.com/t/instanciate-udt-tags-with-the-sdk/4556):
>
> Hi, Is there a way with the SDK to instanciate UDT tags : I need to be able to create some new equipement from an existing type (defined by an UDT), on the fly from a sql table witch contains the new equipement to add.

---

<div class="post-metadata">

**Author:** ![Paul.Scott](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul.scott/32/4578_2.png) [@Paul.Scott](https://forum.inductiveautomation.com/u/Paul.Scott)\
**Post date:** [July 20, 2017, 9:38pm UTC](https://forum.inductiveautomation.com/t/add-instance-of-udt-from-a-tag-valuechanged-event-script-in-another-udt/14840/2 "2017-07-20T21:38:05Z")

</div>

I see that you’ve included the tag provider (I’m assuming `[Default]` is the tag provider) as part of the UDTParentType key on line 14 of lineSpec’s script. Have you tried removing that?

It’s assumed that UDT instances are always in the same tag provider as their definition: otherwise the instance wouldn’t be able to reference the configuration on the definition. I’m thinking the error you’re seeing is accurate: the script can’t locate the definition due to the provider messing/confusing the reference to the definition.

Try just `myUDTs/line` for the parent type.

---

<div class="post-metadata">

**Author:** ![jsorlie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jsorlie/32/3645_2.png) [@jsorlie](https://forum.inductiveautomation.com/u/jsorlie)\
**Post date:** [July 20, 2017, 11:44pm UTC](https://forum.inductiveautomation.com/t/add-instance-of-udt-from-a-tag-valuechanged-event-script-in-another-udt/14840/3 "2017-07-20T23:44:20Z")

</div>

**re: included [Default] as part of the UDTParentType key - Have you tried removing that?**

I've tried it both ways. The behavior is the same. What prompted me to try this was the `system.tag.*` documentation. Several places therein it stipulates the need to specify tag provider for scripts running in the gateway scope.

Here is XML for the UDT's with two lineSpec tags - one with [Default] and one without - in the UDTParentType value:

- [tags-myUDTs-v2.xml](https://us1.discourse-cdn.com/inductiveautomation/original/2X/6/6ee90345ca69a068885aec0fec25b6b3c2109b20.xml) (4.3 KB)

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/31210a56bbcaf76aa92b802a1ea3c443720a55dc.png)

In an instance of the revised `index` UDT definition, changing the value of either `lineSpec` or `lineSpec1` tags yields the same exception:

> UDT [Default]\_types\_/myUDTs/line does not exist. Can not create instance.,

**re: It's assumed that UDT instances are always in the same tag provider as their definition**

Both UDT's are defined in the [Default] tag provider. Likewise the intention is for all instances (of both tags) to be in the default tag provider.

**re: Try just myUDTs/line for the parent type.**

If I understand what you mean, I think this is demonstrated above in the revised MyUDTs/index definition.

* * *

I expect the exception is thrown by the Java object constructor for the new `line` UDT instance. The fact that this constructor is executed/triggered from within the context (scope) of the `index` UDT instance is the problem.

This leads me to suspect that datatype definitions are not included in a UDT instance's "view" of its tag provider (i.e. the tag provider it hosted within).

Is there any way we could test this?

Can anyone point me to some example SDK code that might enable me to build a work-around?

* * *

BTW: One might contemplate building a work around using a gateway event script. Problem with this is these scripts are still scoped to the project. The tags have global scope of the gateway. One could further envision an SFC to monitor these global tag events. Rube Goldberg would be impressed...

---

<div class="post-metadata">

**Author:** ![Paul.Scott](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul.scott/32/4578_2.png) [@Paul.Scott](https://forum.inductiveautomation.com/u/Paul.Scott)\
**Post date:** [July 21, 2017, 12:14am UTC](https://forum.inductiveautomation.com/t/add-instance-of-udt-from-a-tag-valuechanged-event-script-in-another-udt/14840/4 "2017-07-21T00:14:00Z")

</div>

@jsorlie What version of Ignition are you using? I took the tag export in your last post and was able to create an instance of your `line` UDT without modifying your script. For the record, the gateway I tested this on is 7.9.4 beta 2.

Note that both of your lineSpec tags work: looks like the tag provider in the UDTParentPath doesn't matter in this case.

> [@jsorlie](#):
>
> Can anyone point me to some example SDK code that might enable me to build a work-around?

Unfortunately I can't help you here, but I don't see why this shouldn't work out of the box. Perhaps someone else can chime in on this.

---

<div class="post-metadata">

**Author:** ![jsorlie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jsorlie/32/3645_2.png) [@jsorlie](https://forum.inductiveautomation.com/u/jsorlie)\
**Post date:** [July 21, 2017, 3:29am UTC](https://forum.inductiveautomation.com/t/add-instance-of-udt-from-a-tag-valuechanged-event-script-in-another-udt/14840/5 "2017-07-21T03:29:40Z")

</div>

We're running on release 7.9.3 .

Thanks for bearing with me and for testing the `line` UDT works. Of course manual creation of `line` instances is not the objective. The goal is to do this in the tag-change event script in the `index` UDT instance.

Such a mechanism would allow each `index` instance to locally maintain the tag folder it is created within. By maintain I mean creating instances of other UDT definitions. Here I created the "toy" `line` UDT definition purely to prototype the mechanism.

**re: Note that both of your lineSpec tags work: looks like the tag provider in the UDTParentPath doesn't matter in this case.**

Yes you can change the value in either of these tags (`lineSpec` or `lineSpec1`) in an instance of the `index` UDT. If you do so and then check the gateway log (eg. [http://localhost/main/web/status/diag.logviewer](http://localhost/main/web/status/diag.logviewer) ) you will see the exception thrown in the tag valueChange event scripts.

The desired behavior would be to create a `line` UDT instance named per the `lineSpec` or `lineSpec1` value (of course depending on which one you change..) if it does not already exist. Again the difference between the two lineSpec tags is that one explicitly includes [Default] in the `UDTParentType` specification in the `system.tag.addTag()` invocation.

* * *

The second tag.XML upload in my original post contains instances of both UDTs inside a folder named `site` (eg. `[]site/area1/index`).

[tags-site.xml](https://us1.discourse-cdn.com/inductiveautomation/original/2X/9/99235955016d8a27d6d5a178b91ec181130332af.xml) (479 Bytes)

[tags-myUDTs-v2.xml](https://us1.discourse-cdn.com/inductiveautomation/original/2X/6/6ee90345ca69a068885aec0fec25b6b3c2109b20.xml) (4.3 KB)

To experiment with the UDTs, both XML files need to be imported.

* * *

Could it be that one needs to set some of the other [tag attributes](https://docs.inductiveautomation.com:8443/display/DOC79/Tag+Attributes) besides `UDTParentType`:

> attributes={  
> "UDTParentType":"[Default]myUDTs/line"  
> },
