How to iterate a list to find a matching item and its index with smartvalues
Asked by Adolfo Casari:

Hi,

I have a variable {{X}} with a list of strings.

Given a smart value {{Y}} (string), I need to find whether there is a matching value in the list X AND its position.

I know how to iterate X and check if Y matches any element, but I don't know how to get the index with the match.

if there is a matching, the idea is then to do {{X.get(index+1)}} and to handle the case when index+1 is out of bound.

Thanks for your help!

NOTE: I am trying this:

```
Result: {{#NombresdeTareas}} {{#if(equals({{.}}, issue.summary))}} {{index}} {{/}} {{/}}
```

but fails with this error:

Could not extract smart value parameter: {{.: Result: {{#NombresdeTareas}} {{#if(equals({{.}}, issue.summary))}} {{index}} {{/}} {{/}}


Answers:
author: Bill Sheboy
body: Hi [@Adolfo Casari](/t5/user/viewprofilepage/user-id/853257)

Just to confirm, is your {{X}} a created variable or an issue field? If it is a created variable, you would need to split it into a list before it can be iterated upon.

For the other part of your question, I am assuming you are using a created variable...

Created variables are text, and have no object type or attributes. And so when split a value is just the {{.}} as you have shown. However that cannot be referenced in the smart value functions. Instead many of the functions are implemented to handle that case without passing the value.

For example...

* action: create variable
  * name: varMyVariable
  * value: red,green,blue,alpha
* action: log a search for a value, "blue"
  * {{#varMyVariable.split(",")}}{{#if(equals("blue"))}}found {{.}} at {{index}}{{/}}{{/}}

A complication in your scenario is that once inside of an iterator, only data at that scope and below is visible. That means that for your iteration on NombresdeTareas, the issue summary is not visible.

One work-around for that is to use the match() function with a regular expression on the variable instead of a conditional inside of the iterator:

{{varMyVariable.split(",").match("(blue)")}}

If you do need the index value from the list, would need to store the index with the value, and include that in your match expression.

Kind regards,  
Bill

comments: author: Adolfo Casari
body: Hi [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313)
> ust to confirm, is your {{X}} a created variable or an issue field? If it is a created variable, you would need to split it into a list before it can be iterated upon

It's a created variable.

> A complication in your scenario is that once inside of an iterator, only data at that scope and below is visible. That means that for your iteration on NombresdeTareas, the issue summary is not visible.

Yes, I found out that. I got an exception error when using issue.summary in the iterator. Any hope for that limitation to get fixed in the future? It's a very bad thing not being able to use any issue fields inside an iterator.

> If you do need the index value from the list, would need to store the index with the value, and include that in your match expression.

So your saying red-1,green-2,blue-3,alpha-4 right? Can use match with a list? I don't see that option here: <https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/>

Thanks a lot for your help.


author: Adolfo Casari
body: I tried your suggestion but seems that I can't use issue.summary inside the match. The idea is to match the current issue summary againts a list of predefined issue summaries, so I can create the next issue in the list.


author: Bill Sheboy
body: I am just a Jira user, and so have no visibility to Atlassian changing the scoping behavior for iterators.

Regarding using the index values with the list, first you build the list with an iterator storing in a variable, as you showed for my example. Then match on the value, such as "(blue\*)". And finally separate out the value with text functions to get your index.

On your next post about using a field like {{issue.summary}} in a match's regular expression, the trick is to first create a variable with the expression, and then use that in the match:

* action: create variable
  * name: varRegularExpression
  * value: .\*({{issue.summary}}).\*
* action: log on the match
  * {{varMyVariable.split(",").match(varRegularExpression)}}

Please adjust the expression / grouping as needed for your scenario.


author: Adolfo Casari
body: [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313)

One more thing, do you know if list.get(index) works when index is a smartvalue? I just can't make it work, I saved the index in a variable (myIndex) and logs the right number, but with get fails.

Tried get({{myIndex.asNumber}}) but that fails too:
>
> ```
> Failed to get value for ParseTareas.split(",").get({{PosicionTareaActual.asNumber.plus(1): {{ ParseTareas.split(",").get({{PosicionTareaActual.asNumber.plus(1)}}) }}
> ```


author: Bill Sheboy
body: At this time, there are at least two versions of the get() function for automation rules: for lists and for lookup tables.

1. The version of get() for lists cannot take a smart value as a parameter. (This is what you are trying to do, although you appear to have some typos in your expression.)
2. The version for lookup tables can take literal values, smart values, and function results.


author: Adolfo Casari
body: [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313)Thanks for clarifying.

I am under scenario 1.

With your help I was able to iterate the list to find a match for issue.summary, but I don't have a way to get the next item from the list.


author: Bill Sheboy
body: I am getting confused as to what is the *source* and the *search criteria* for your scenario, as you appear to be noting multiple different fields.

Would you please post images of:

* your current, complete automation rule in one image
* images of each action / condition / branch details
* an image of the audit log details showing the rule execution

Those will provide context and help us to have a shared understanding of what you are observing. Thanks!


author: Adolfo Casari
body: [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) the whole rule is too long, but here is the issue:

1) I create a variable and log its value (I get 1)

![imagen.png](https://community.atlassian.com/t5/image/serverpage/image-id/285743iE3AC06D9142817DC/image-size/large?v=v2&px=999 "imagen.png")

2) I use it to get the next item in the list.

![imagen.png](https://community.atlassian.com/t5/image/serverpage/image-id/285745iAC159F35C300870B/image-size/large?v=v2&px=999 "imagen.png")

Log SiguienteTarea and get nothing.

If you put get({{ProximoIndex}}) get an error. If you put get(1) you get the next element. Tried also ProximoIndex.asNumber with no luck.


author: Bill Sheboy
body: The get() function for a list can only take a number for a parameter. It cannot take a smart value, either from a field or from a created variable.

If you review my earlier posts, I described how to perform this type of search using a match() function and a dynamic search expresssion.

When your rule is longer, you can try using either a screen capture or a browser addon which can scroll to grab the entire image.


author: Adolfo Casari
body: [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313)

I was able to come up with the regex to capture the next item from the list after the match with issue.summary

Thanks a lot for your help with this post.


Tags: jira-cloud