Use result of match function in smart value branching / for each loop

johannesm April 28, 2022

I have an automation rule in which I:

  • Make a web request
  • Execute `match` to extract multiple occurrences of a pattern from the response
  • Branch-iterate over the occurrences to do something else (e.g. use the matched substring in a JQL query)

Screenshot 2022-04-28 at 13.43.44.png

The problem is the "for each: smart value" branching doesn't actually iterate over the matches but instead treats the result of `match` as a single value.

Screenshot 2022-04-28 at 13.48.33.png

Does anyone know how I could loop over the matches one by one?

3 answers

1 accepted

4 votes
Answer accepted
johannesm April 28, 2022

As mentioned in my reply above, the audit log smartly collapses log messages which creates the impression that the whole list is a single string. It is, in fact, not though which means you can use operate on the element inside the branch exactly like you'd expect.

Screenshot 2022-04-28 at 21.56.18.png

Thanks everyone for the quick help! This is a great community.

6 votes
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 28, 2022

Hi @johannesm 

I find that advanced branching occasionally needs "help"; sometimes it parses the source as a list and sometimes not.  Two approaches for your source list are:

  • save the match() result in a created variable (which forces to text), and feed that into the advanced branch using split(",")
{{varMyBranchSource.split(",")}}
  • Or, immediately iterate on the match result in the advanced branch source, forcing it to a list explicitly.  For example:
{{#webResponse.body.body.match(".*(#[0-9]*).*")}}{{.}}{{/}}

And also...I highly encourage you to always make variable names (created or branch) different from all possible reserved words.  In your advanced branch you call it match and I would suggest always prefixing, such as varMatch.

Kind regards,
Bill

Filipi Lima
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 28, 2022

Thanks for the master class, @Bill Sheboy !

johannesm April 28, 2022

First of all, thanks for pointing out that creating a variable forces to text. I was not aware and that explains some failures I saw while testing other things.

I tried both your suggestions but the behavior wasn't any different. One thing I did notice during testing though is that the audit log appears to smartly collapse log messages.

When I used

>{{varMatch}}<

I noticed the angular brackets being added to each element in the output.

Screenshot 2022-04-28 at 21.48.47.png

So ultimately, the audit log just makes it seem as if the value is a single string by collapsing the log messages into a single line.

Which means it worked correctly from the start and I just didn't realize because the log output kept me from trying to proceed. 🤦‍♂️

Like # people like this
Filipi Lima
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 28, 2022

We've all been there.. I confess I missed it completely, too.

Regardless, I'm glad to hear it's working!

1 vote
Filipi Lima
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 28, 2022

Hello! Welcome to the community!

One thing worth trying is figuring out whether the output of your regex is a list or a string - if it was a list I would expect it to work on your setup so let's check.

To do that, try logging one of these on the audit log:

{{match.first}}

{{match.get(3)}}

Taken from: https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/

If your match variable is a string, we'll probably get an error on the above and you'll need to .split your regex output to create a list.

Look for the .split function here: https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/

Let us know the output of the audit log tests.

johannesm April 28, 2022

Thanks for your help Filipi!

It seems like the output of calling `match` is a list because calling `first` on it gives the first element.

However, inside the for each branching I appear to get the whole list as a single string and calling `first` there does nothing.

Screenshot 2022-04-28 at 15.23.41.png

Mykenna Cepek
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 28, 2022

Now that you know it's a list, you can iterate on the list as a Smart Value (not in a branch). I'm not sure if that will achieve your goals, but a lot of list processing can be done with Smart Value notation. See the Combined function examples.

For more complex handling in Smart Value lists, check out the conditionals you can use while iterating on a list.

Note that all these will do some processing of the list, resulting in some text that you would use elsewhere (e.g. in an email, or to set a variable). You don't actually get to manipulate the list.

Like johannesm likes this
johannesm April 28, 2022

Thanks Mykenna! This made me realize that I could "unroll" the branch-loop and instead manually access list elements by index. This ends up with a quite lengthy automation rule but at least works if you know a reasonable finite upper limit for the length of the list.

Screenshot 2022-04-28 at 21.31.44.png

Like Mykenna Cepek likes this

Suggest an answer

Log in or Sign up to answer