# Barcode Data Handling Script Write to Indirect Tag

**URL:** <https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187>\
**Category:** Ignition\
**Created:** [August 29, 2020, 1:12am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187 "2020-08-29T01:12:39Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![beckya](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@beckya](https://forum.inductiveautomation.com/u/beckya)\
**Post date:** [August 29, 2020, 1:12am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/1 "2020-08-29T01:12:40Z")

</div>

I watched this video, but I need a script to write to tags indirectly  
I need the barcode data to write to the tag based on the machine number. I know how to write that in an expression but am not sure how to write it in the script. The machine number is currently on {view.params.MachineNumber} property.  
The tag is Machine 01/Batch Number. I tried replacing the 01 with {view.params.MachineNumber} but no luck.

> `system.tag.writeAsync(['[default]Machine {view.params.MachineNumber}/Batch Number'], [data.text])

I also tried

> system.tag.writeAsync(['[default]Machine {MachineNumber}/Batch Number'], [data.text])   
> MachineNumber = {view.params.KettleNumber}

I’m using flex repeater instances of a machine template.

> **[Barcode Scanned Video at Inductive University](https://www.inductiveuniversity.com/video/barcode-scanned/8.0?r=%2Fvideo%2Fsearch%2F%3Fq%3Dbarcode%20scanner%26checked_topics%3D)**
>
> Learn how to use the Ignition Perspective App to process a scanned barcode on a mobile device.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 30, 2020, 7:06pm UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/2 "2020-08-30T19:06:21Z")

</div>

Youre supplying the literal tagpath ’[default]Machine {view.params.MachineNumber}/Batch Number’ to the tag write function. You need to evaluate the machine number part first and then add it to the string:

`'[default]Machine %s/Batch Number' % (view.params.MachineNumber)`

Ps. You may need some other parts to that view.params reference. Use the property selector to get the right reference to it

---

<div class="post-metadata">

**Author:** ![beckya](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@beckya](https://forum.inductiveautomation.com/u/beckya)\
**Post date:** [August 30, 2020, 11:56pm UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/3 "2020-08-30T23:56:12Z")

</div>

> [@nminchin](#):
>
> % (view.params.MachineNumber)

Thank you for the reply.

I tried

> ` system.tag.writeAsync(['[default]Machine %s /Batch Number' % (view.params.MachineNumber)], [data.text])

and I confirmed that there is no other part to the view/params reference. It did not work.

The view params is from the Machine template that is being repeated in the flex repeater. When I scan a barcode from the flex repeater it does not go to anywhere. If I put a single machine tag it works, so I know it can work, but I need it to be dynamic to see the machine number and apply it to the machine's tag based on the view that the Scan Barcode button is being opened on.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 31, 2020, 12:02am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/4 "2020-08-31T00:02:11Z")

</div>

Sorry, my phone used the wrong apostrophes… or rather, I chose the wrong apostrophe on my phone.  
You need to use `'` instead of `’`. I fixed it in my post.

---

<div class="post-metadata">

**Author:** ![beckya](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@beckya](https://forum.inductiveautomation.com/u/beckya)\
**Post date:** [August 31, 2020, 12:04am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/5 "2020-08-31T00:04:18Z")

</div>

> [@beckya](#):
>
> system.tag.writeAsync([’[default]Machine %s /Batch Number’ % (view.params.MachineNumber)], [data.text])

I apologize, I did have the right apostrophe, this just does not like to show it that way. It did not work

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 31, 2020, 12:06am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/6 "2020-08-31T00:06:24Z")

</div>

I just checked in my Designer and I’m pretty sure you need to use:  
`self.view.params.MachineNumber`

Also, for all your code on the forum, you should wrap it inside preformatted text characters. Either ``` for multiple lines or ` for single lines.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/7/27ad0ba74a6ca78f94a62e7bed2a026b355610c1.png)

---

<div class="post-metadata">

**Author:** ![beckya](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@beckya](https://forum.inductiveautomation.com/u/beckya)\
**Post date:** [August 31, 2020, 12:36am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/7 "2020-08-31T00:36:11Z")

</div>

Thank you. I tried adding self. and it still will not write the barcode to any tags.

---

<div class="post-metadata">

**Author:** ![beckya](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@beckya](https://forum.inductiveautomation.com/u/beckya)\
**Post date:** [August 31, 2020, 12:55am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/8 "2020-08-31T00:55:50Z")

</div>

This is the error

> ```auto
> 
> ```

Error running session.onBarcodeDataReceived(session, data, context): Traceback (most recent call last): File "function:onBarcodeDataReceived", line 3, in onBarcodeDataReceived at com.inductiveautomation.ignition.common.tags.paths.parser.TagPathParser.\_parse(TagPathParser.java:193) at com.inductiveautomation.ignition.common.tags.paths.parser.TagPathParser$ParseFunction.load(TagPathParser.java:365) at com.inductiveautomation.ignition.common.tags.paths.parser.TagPathParser$ParseFunction.load(TagPathParser.java:360) at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3708) at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2416) at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2299) at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2212) at com.google.common.cache.LocalCache.get(LocalCache.java:4147) at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4151) at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:5140) at com.inductiveautomation.ignition.common.tags.paths.parser.TagPathParser.parse(TagPathParser.java:149) at com.inductiveautomation.ignition.common.script.builtin.AbstractTagUtilities.parseTagPath(AbstractTagUtilities.java:81) at com.inductiveautomation.ignition.common.script.builtin.AbstractTagUtilities.parseTagPaths(AbstractTagUtilities.java:92) at com.inductiveautomation.ignition.common.script.builtin.AbstractTagUtilities.writeAsync(AbstractTagUtilities.java:370) at jdk.internal.reflect.GeneratedMethodAccessor449.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) com.inductiveautomation.ignition.common.tags.paths.parser.TagPathFormatException: com.inductiveautomation.ignition.common.tags.paths.parser.TagPathFormatException: Token PROPERTY\_SEPARATOR found after property name.

```auto

```

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 31, 2020, 1:21am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/9 "2020-08-31T01:21:51Z")

</div>

> [@beckya](#):
>
> [default]Machine %s /Batch Number’

Should there be a space after %s?  
If that fails, before you write to the tag, write the tag path string you're creating to a component property or print it (when you use print in Perspective it will go into the wrapper.log, so make sure you have something in there unique to search for).  
E.g.

```auto
print '[default]Machine %s/Batch Number' % (self.view.params.MachineNumber)

```

Also, preformatted characters should be `, not an apostrophe. It's the same key that tilde ~ is attached to (on QWERTY layout keyboards)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/8/680c69df99c6405c01d036feebdf469ba00452b3.png)

---

<div class="post-metadata">

**Author:** ![beckya](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@beckya](https://forum.inductiveautomation.com/u/beckya)\
**Post date:** [August 31, 2020, 1:37am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/10 "2020-08-31T01:37:02Z")

</div>

thank you. No, there is no space in the tag path after the machine number

---

<div class="post-metadata">

**Author:** ![beckya](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@beckya](https://forum.inductiveautomation.com/u/beckya)\
**Post date:** [August 31, 2020, 2:17am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/11 "2020-08-31T02:17:38Z")

</div>

> [@nminchin](#):
>
> self.view.params.MachineNumbe

This is the error I get:  
`	Error running session.onBarcodeDataReceived(session, data, context): Traceback (most recent call last): File "<function:onBarcodeDataReceived>", line 2, in onBarcodeDataReceived NameError: global name 'self' is not defined`

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 31, 2020, 2:50am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/12 "2020-08-31T02:50:55Z")

</div>

Sorry, I misunderstood where you were calling this from.  
You can’t read the view’s properties from a session script. What I would do is to create a session property and bi-directionally bind the MachineNumber that you select to that. Then in the `onBarcodeDataReceived` script, you’ll be able to reference it using e.g. `session.custom.MachineNumber`

---

<div class="post-metadata">

**Author:** ![beckya](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@beckya](https://forum.inductiveautomation.com/u/beckya)\
**Post date:** [August 31, 2020, 3:10am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/13 "2020-08-31T03:10:18Z")

</div>

Aha, thanks. I created the session property MachineNumber but I’m not sure where to bind it. When you say the MachineNumber that you select do you mean the Scan Barcode button?

I have the Scan Barcode button on the Machine template as well as the text field that has a bi-directional indirect binding using the view params MachineNumber to the tag I’m trying to write to.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 31, 2020, 3:45am UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/14 "2020-08-31T03:45:32Z")

</div>

Yes, you would write to it when you click on the scan barcode button. Instead of using your view param on the label text binding, you would use the session property and remove the view param altogether.

---

<div class="post-metadata">

**Author:** ![George\_El-Hachem](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@George\_El-Hachem](https://forum.inductiveautomation.com/u/George_El-Hachem)\
**Post date:** [June 23, 2022, 6:36pm UTC](https://forum.inductiveautomation.com/t/barcode-data-handling-script-write-to-indirect-tag/38187/15 "2022-06-23T18:36:19Z")

</div>

Hello, I’m having a similar problem here where I’m trying to build an inventory system using barcodes. I have a scan barcode button, and have “Quantity” tags linked to a value in a table. I wrote the script in the video but it seems like all barcodes change one output in my table. How do I write a dynamic script so that each barcode corresponds to specific tags in my system?
