Jira Automation: Iterate over labels and use lookup table to map label name to an email address
Asked by Jonathan Scott:

I'm trying to use Jira global automation rules to loop over the labels on a specific task when it is created/updated. If the label name exists in a lookup table (departmentEmails), then append/contact the value to another variable called "toEmails". If the label does not exist in the lookup table, but it contains an "@" symbol, also prepend/contact the label name to "toEmails".

I can not for the life of me get the lookup table to use departmentEmails.get(.) on the iteration of the labels. I've tried several ways, like this (using the THEN set variable command):

```
{{#issue.labels}}{{#if(departmentEmails.get(.))}}{{departmentEmails.get(.)}}; {{/}}{{/issue.labels}} 
```

but that yields an error about the ".", which is the only way I know to access the current iteration in the nested loop.

I even tried not using the lookup tables, but I also get an error with this:

```
{{toEmails}}{{#issue.labels}}{{#if(equals(., "Marketing"))}}marketing@my-company.com; {{/}}{{/issue.labels}}
```

Can anyone come up with a better way to do this? I would use the BRANCH action on a smart value but I need to also do some if/then/else logic, and that can't be done within a branch. Thank you for your help.


Answers:
author: Bill Sheboy
body: Hi [@Jonathan Scott](/t5/user/viewprofilepage/user-id/5562150) -- Welcome to the Atlassian Community!

One of the limitations of automation rules is: *once inside of an iterator, no data from outside is visible / available.* That is:

```
{{#issue.labels}} ...inside the iterator, can only see labels, and lower... {{/}}
```

And so your lookup table is not visible.

A workaround for this scenario is not to use the lookup table to perform the matching directly, but instead search the table using a regular expression built from the labels with the [match() function](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/#match--). This would produce a list of values to append to *toEmails*.

First, we build something to search: a delimited string built from the table, using the [entries feature](https://community.atlassian.com/t5/Automation-articles/Update-Create-lookup-table-action-improvements/ba-p/2427798) of lookup tables with a [list iterator.](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/#Combined-function-examples) Let's store that in a variable named varDepartmentData.

```
{{departmentEmails.entries}}{{key}}~~{{value}}{{^last}};{{/}}{{/}}
```

That will produce something like this:

```
DepartmentA~~departmentAemailAddress;DepartmentB~~departmentBemailAddress
```

To search that text, we build a regular expression from the labels. Let's name that variable varSearchExpression.

```
({{#issue.labels}}{{.}}~~.*{{^last}}|{{/}}{{/}})
```

If the labels where "DepartmentC, DepartmentA", that will produce something like this:

```
(DepartmentC~~.*|DepartmentA~~.*)
```

Putting those together, we can search and extract the result with this:

```
{{varDepartmentData.split(";").match(varSearchExpression).substringAfter("~~")}}
```

Then add other list / text functions as needed to add to your other variable.

Kind regards,  
Bill

comments: author: Jonathan Scott
body: Thank you so much Bill, that worked perfectly!


author: Bill Sheboy
body: Awesome; I am glad to learn that helped.

Please consider marking this question as *answered* to help others in the community with similar needs find solutions faster. Thanks!


Tags: cloud

jira-cloud