# How to get selected instance name in flex repeater

**URL:** <https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246>\
**Category:** Ignition\
**Tags:** ignition81, perspective, scripting\
**Created:** [November 22, 2023, 11:08am UTC](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246 "2023-11-22T11:08:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Priyanka.Khandge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/priyanka.khandge/32/35989_2.png) [@Priyanka.Khandge](https://forum.inductiveautomation.com/u/Priyanka.Khandge)\
**Post date:** [November 22, 2023, 11:08am UTC](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246/1 "2023-11-22T11:08:48Z")

</div>

Hi guys,

Any idea how to get the selected instance name in Flex repeater?

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/4/e40dfd1af7ffacb2de483407f03be60d1804469c.png)

Like when I select Instance[25] then get the role name in the custom parameter.

Thanks,  
Priyanka

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 22, 2023, 11:19am UTC](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246/2 "2023-11-22T11:19:30Z")

</div>

What are the array items 24 - 28? Are they flex repeater instance view parameters? (I'm confused because when I create a flex repeater each instance has instanceStyle and instancePosition properties and yours does not. Did you create the array by script?)

---

<div class="post-metadata">

**Author:** ![Priyanka.Khandge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/priyanka.khandge/32/35989_2.png) [@Priyanka.Khandge](https://forum.inductiveautomation.com/u/Priyanka.Khandge)\
**Post date:** [November 22, 2023, 11:27am UTC](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246/3 "2023-11-22T11:27:15Z")

</div>

I have created a flex repeater instance using a named query. Named query return format in JSON.

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

---

<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:** [November 22, 2023, 11:34am UTC](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246/4 "2023-11-22T11:34:30Z")

</div>

> [@Transistor](#):
>
> instanceStyle and instancePosition

You don't need either of these, I usually delete them if I don't use them

These params will get pass automatically into your embedded Views as parameters. So if you have role and selected params in your embedded View, then the values will appear in them

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 22, 2023, 11:52am UTC](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246/5 "2023-11-22T11:52:46Z")

</div>

> [@Priyanka.Khandge](#):
>
> Any idea how to get the selected instance name in Flex repeater?

If you want the main view to know which repeater view has been selected then I would use [system.perspective.sendMessage](https://docs.inductiveautomation.com/display/DOC80/system.perspective.sendMessage) in an onClick event on the root of the embedded view.

```auto
def runAction(self, event):
	role = self.view.params.role
	system.perspective.sendMessage("repeaterViewClicked", {"role": role}, scope = "page")

```

Then use a message handler on the component in the main view that needs the information. Be sure to set the listen scope to "Page".

---

<div class="post-metadata">

**Author:** ![Priyanka.Khandge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/priyanka.khandge/32/35989_2.png) [@Priyanka.Khandge](https://forum.inductiveautomation.com/u/Priyanka.Khandge)\
**Post date:** [November 23, 2023, 6:20am UTC](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246/6 "2023-11-23T06:20:46Z")

</div>

> [@Priyanka.Khandge](#):
>
> Any idea how to get the selected instance name in Flex repeater?

Resoved using the script below. I have binding the script below in custom property.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/e/9e00bf129f4e3d945b4e01094e389cb83cbc252d.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/1/910cffde52d09fbd2704b3be55def9c54ade06a2.png)

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [November 23, 2023, 11:25am UTC](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246/7 "2023-11-23T11:25:31Z")

</div>

May I suggest a slight rework of this script ?  
Because frankly this is a weird and convoluted way of doing it.

```python
return next(instance.role for instance in value if instance.get(selected))

```

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [November 23, 2023, 12:23pm UTC](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246/8 "2023-11-23T12:23:43Z")

</div>

> [@pascal.fragnoud](#):
>
> frankly this is a weird

For some reason this made me remember the words of an old mentor. He would say: "Where were you when the page was blank!"

🤣🤣🤣

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [November 23, 2023, 1:23pm UTC](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246/9 "2023-11-23T13:23:42Z")

</div>

I didn't have much time earlier, but now I'd like to expand a bit.

There's a slight difference in functionality between both our scripts. It stems from an assumption I made, which is that only one instance should be selected at a time.  
So, my script returns the role associated with the first selected instance encountered.  
Yours will return the last one.  
Which is equivalent if only one is selected.  
If you DO want the last one, then you can use the script I provided, with one small change: reverse the order of `value`:

```python
return next(instance.role for instance in reverse(value) if instance.get(selected))

```

Now, why do I think your script is weird and convoluted ?  
You want to return ONE thing, but you're building a list. A list from which you pick the last element, every time you append something.  
There are a few things that are really, really unnecessary about this.

1. You're appending an element to a list, then picking the last element from the list.  
=\> The list is completely useless here. why not just do `val = instance.role` and get rid of the list ?
2. You're returning the last element of a list, but picking it every time the list is modified  
=\> You could take this step out of the loop, and use `return rolelist[-1]` instead
3. Either there's only one instance selected, or you want to return the role associated with the last one. In both cases, there's a better way to do it:  
=\> If there's only one, why build a list ? You could just return the role when you find the selected instance: remove the list and replace the append with `return instance.role`  
=\> if you want the last one, start from the end of the instances list and return the first one you find

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [November 23, 2023, 4:56pm UTC](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246/10 "2023-11-23T16:56:29Z")

</div>

While you may have "resolved" the issue with the post currently marked as a solution, this post is the correct way to manage this: [How to get selected instance name in flex repeater - #5 by Transistor](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246/5)

With the code provided and a configured Message Handler listening for `repeaterViewClicked` you'll be able to configure a script to execute any time a user clicks an instance.

The code provided in [this post](https://forum.inductiveautomation.com/t/how-to-get-selected-instance-name-in-flex-repeater/82246/6) is NOT a good way to go about this as you'll always get the instance with the highest index, even if it was not the most recent instance selected.
