A4J Cloud - Advanced field editing match()
Asked by Neil Taylor:

Hello,

I am trying to get the match() statement to work in Advanced Field editing in a Jira Cloud Automation.

You can see what I have below - the first if I have as a match and it is not working. The second if is an equals and it works without any issue. Any ideas for what might need changed to get the match to work? I've tried quite a few variations to get the regex working as expected, but I must be missing something.

(In Edit Issue action - advanced editing)

{ "update": { "customfield_16000": \[ { "set": {"value": "{{#if(issue.summary.toLowerCase.match(\\".\*(crash).\*\\"))}}Crash{{/}}{{#if(equals(issue.summary.toLowerCase(), \\"hang\\"))}}Hang{{/}}"} } \] } }

There are no useful errors - essentially if i set my summary to "crash", it doesn't match either if in the statement so that is the only error going on that I can see in the audit logs.

Here's the match documentation I was going off of: <https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/#match-->

TIA!


Answers:
author: Darryl Lee
body: Got it! I named my custom field **Cause** (you don't have to use customfield_xxx, which makes rules easier to read).

This worked for me:
> { "update":   
> { "**Cause**": \[ { "set": {"value":
>
> {{#if(issue.summary.toLowerCase.match(".\*(crash).\*").isNotEmpty)}}"Crash"{{/}}   
> {{#if(issue.summary.toLowerCase.match(".\*(hang).\*").isNotEmpty)}}"Hang"{{/}}  
> {{#if(issue.summary.toLowerCase.match(".\*(foo).\*").isNotEmpty)}}"Foo"{{/}}  
> {{#if(issue.summary.toLowerCase.match(".\*(bar).\*").isNotEmpty)}}"Bar"{{/}}  
> } } \] }  
> }

Please keep in mind though that regexes are notoriously fickle things. You'll want to make SURE that you don't have causes that contain the same words.

Silly example: Was it a **crash** , or a **rash** , or **ash** that caused the problem?

Another problem. If the summary mentions more than one keyword, it will change your custom field to the last option you check. Example: "This crash was so bad. It made the computer Hang multiple times. Foo. Bar."

But I guess if your users are aware of the limitations of this approach, it should work. Neat idea, and fun to work out. Thanks for thinking of it.

P.S. OH, one thing I thought of is the possibility that somebody doesn't use one of your 60 (!) possible causes, or forgets.

Since Automation doesn't have an ELSE operator, you can't set up a final rule to choose "Other" or something like that. Instead I think the rule will end up erroring out.

OH, I just read more in [Jira smart values - conditional logic](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-conditional-logic/), and thought i could do it with an AND statement that contained with all the possibilities words coming up empty, but I'm not having much luck with that... Still hacking on it a bit.

comments: author: Darryl Lee
body: Ok, didn't need the AND, since I can do regex alternation using the pipe (\|) character. But I **did** need the NOT operator because Automation's **isEmpty** function does not work very well, it seems.

So I added this ugly line after the other IF statements:
> {{#if(not(issue.summary.toLowerCase.match(".\*(crash\|hang\|foo\|bar).\*").isNotEmpty))}}"Other"{{/}}  
> }

I do advise you to type your rules out in a text editor (I'm a fan of vim), so you can check your parentheses, etc. and even format rules with proper indentation, etc. Makes it a lot easier to troubleshoot and find typos, etc.

For completeness, here's the reference for text functions like **isEmpty** , **match** , and **isNotEmpty**:

[Jira smart values - text fields](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/)


author: Neil Taylor
body: Thank you [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) ! I tested initially and that fixed the issue I was running into! You bring up some good points as well that I will look into


author: Darryl Lee
body: Oh wow. After figuring out the fix for "Other", I realized and tested to confirm that this could be done without any IF statements at all, ***IF*** you are willing to have your custom field options be all lowercase:
> { "update":   
> { "Cause": \[ { "set": {"value":   
> "{{issue.summary.toLowerCase.match(".\*(crash\|hang\|foo\|bar).\*")}}{{#if(not(issue.summary.toLowerCase.match(".\*(crash\|hang\|foo\|bar).\*").isNotEmpty))}}other{{/}}"  
> } } \] } }

Note that the line with the matching logic must all be on one line with no spaces, because we are doing what you originally intended, "filling in" what goes between the quote marks for value.

For testing, I set the rule up to trigger on **Summary** change, and when I updated it, the **Cause** field updates after a second or so:

![Screen Shot 2022-11-25 at 12.36.52 AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/227532iC18E991C3ED2DEDA/image-dimensions/359x296?v=v2 "Screen Shot 2022-11-25 at 12.36.52 AM.png")![Screen Shot 2022-11-25 at 12.38.00 AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/227533iC5FF2433662A26D7/image-dimensions/342x297?v=v2 "Screen Shot 2022-11-25 at 12.38.00 AM.png")![Screen Shot 2022-11-25 at 12.37.22 AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/227534i1D28585245B841C6/image-dimensions/358x295?v=v2 "Screen Shot 2022-11-25 at 12.37.22 AM.png")

Fun stuff!

comments: author: Darryl Lee
body: So yeah, for this, you'd just need to generate a pipe-delimited list of your options, and paste it in twice. (Still need an IF/NOT to check for "other".)

The **match** operator is kind of handy since it returns what it finds.


author: Darryl Lee
body: Oh duh, I guess if you really wanted the options to be capitalized, you could convert them BACK with the handy **capitalize**function.
> { "update":  
> { "Cause": \[ { "set": {"value":  
> "{{issue.summary.toLowerCase.match(".\*(crash\|hang\|foo\|bar).\*").capitalize}}{{#if(not(issue.summary.toLowerCase.match(".\*(crash\|hang\|foo\|bar).\*").isNotEmpty))}}Other{{/}}"  
> } } \] } }


author: Darryl Lee
body: Annnnd, just before going to bed last night I had another idea since Atlassian lets you set default values for Smart Values. Could that be used for the "Other" value? Why yes, yes it can.

So then:

```
{ "update": 
?  { "Cause":
?  ? ?[ { "set": 
         {"value": 
            "{{issue.summary.toLowerCase.match(".*(crash|hang|foo|bar).*").capitalize|"Other"}}"
         }
      } ]
   }
}
```

Whew. Ok, I think that's about as good as it gets. :-}


author: Jesse Wisener
body: That is a work of art.


author: Darryl Lee
body: Awww, thanks [@Jesse Wisener](/t5/user/viewprofilepage/user-id/5090331) - and hi!


author: Stefan Salzl
body: Hi [@Neil Taylor](/t5/user/viewprofilepage/user-id/5086559)

the # in using smart values is eigher used for iterating over a number of results or for calculation.

In case it's an if that checks 1 result this is not needed.

see conditions for smart values here:

<https://support.atlassian.com/cloud-automation/docs/jira-smart-values-conditional-logic/>

Try to leave out the ?#' as well as the closing ?{{/}}'

And furthermore:

Which type is the field of you'd like to edit?

Best  
Stefan

comments: author: Neil Taylor
body: Hi Stefan,

Thanks for the input! I tried leaving out the #'s and the closing {{/}}. By removing them, neither IF works as expected. With those added, my equals works (e.g. if my summary is "hang", it sets my target field to "hang") and it's just the "match" not working.

The field type I am setting in this scenario is a single select field which has the values Crash and Hang.

Basically, my end goal here is to make it so a summary could come in with "My website crashed" and it would set the field to crash. I'm really close as if i use equals and have the summary be exact hang, it sets the field, so i just need to extend this so it's able to do a match if it contains my term I'm looking for. I could definitely do this in other ways, but since I will have over 50 terms, thought getting all of it into one action would be cleanest.

Thanks for the assistance and I'm all ears if you have any other ideas.

Neil


author: Stefan Salzl
body: Hi [@Neil Taylor](/t5/user/viewprofilepage/user-id/5086559)

thanks for your detailed description.

Tbh. I'm not very familiar with regex so I just did a quick ?crash research" as I'm having a hard time following your search pattern.

Could you explain why it is

```
\".*(crash).*\"
```

Wouldn't that take the () around the string ?crash" into your search?

Best  
Stefan


author: Darryl Lee
body: Hey guys, just a few notes:

[@Stefan Salzl](/t5/user/viewprofilepage/user-id/4616993) - the hashmark (#) is not just for iterating over lists. It's also used for the conditional **if** statement, and the**{{/}}**is how you close the if block.

You linked to this already (sorry), but [Jira smart values - conditional logic](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-conditional-logic/) describes this, and it in fact works outside of lists.

Re: quotes: Often with regular expressions you need to "escape" your quote marks with backslashes if you're trying to include quote marks "within" another set of quote marks. In this case [@Neil Taylor](/t5/user/viewprofilepage/user-id/5086559) was trying to return the **value**for his custom field in quotes.

Because the JSON is supposed to look like:

```
"value" = "Crash"
```

He was trying to do something like this:

```
"value" = "{{all-the-logic-to-lookup-and-return-value}}"
```

This might have worked, but seemed too complicated to me, so I put the quote marks within separate IF blocks and just repeat it, like so:

```
{{logic-to-check-if-summary-contains-crash}}"Crash"{{/}}
{{logic-to-check-if-summary-contains-hang}}"Hang"{{/}}
```


author: Stefan Salzl
body: Hi [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322)

Oh sorry for the confusion and thanks for clarifying this here. Always happy to learn things in more details :)

Best  
Stefan


author: Curt Holley
body: Hi [@Neil Taylor](/t5/user/viewprofilepage/user-id/5086559)   
Welcome to the community!

I'm not sure exactly what you are trying to achieve, but are you aware of / could you make use of.....the Issues fields condition options around the summary field??  
There are a bunch of options, not just "contains", and you can make use of smart values in the Value section, plus a case match option.  

Worth a try? or have you already??  
![2022-11-24 10_53_55-Automation.png](https://community.atlassian.com/t5/image/serverpage/image-id/227367iFC68B741449133BE/image-size/large?v=v2&px=999 "2022-11-24 10_53_55-Automation.png")

comments: author: Neil Taylor
body: Hi Curt, thanks for the reply. I could definitely do this, but ultimately I will end up with around 60 if's in my statement - I should have mentioned that - so what you suggest is very possible, it would just be a lot of conditions and actions. I was hoping to use what I started above, to accomplish all my conditions and actions in one single action instead.


author: Curt Holley
body: Right! that makes sense. Good Luck! as I have no other suggestions....sorry!


Tags: cloud

jira