How Does Logging with Smart Values Work?
Asked by Lana Laureano:

We're setting up a rule and running into errors. We've added logging to the rule but we're surprised by the results - we're wondering:

How does the following log result in an output of "true, true"? We would expect to log nothing or false, but definitely not true

```
{{#if(not(lookupIssues.Create/Update Automated Tests?.value.startsWith("N")))}} {{lookupIssues.Create/Update Automated Tests?.value.startsWith("N")}} {{/}}
```


Answers:
author: Darryl Lee
body: Edited to add tl;dr - here's the answer that I think Lana was looking for when she clarified:
> If I want to get the sum of "StoryPoints" for all lookup issues where "Create/Update Automated Tests?" is set to "Needed for Release"

**{{#=}}0{{#lookupIssues}}**   
**{{#if(not(Create/Update Automated Tests?.value.startsWith("N")))}} + {{Story points}}{{/}}{{/}}{{/}}**

Although now that I look at that, I think that would \*exclude\* issues "Needed For Release" since that option starts with "N". For your exact example, I think you would want:

**{{#=}}0{{#lookupIssues}}**   
**{{#if(equals(Create/Update Automated Tests?.value,"Needed for Release"))}} + {{Story points}}{{/}}{{/}}{{/}}**

++Everything below is the long journey it took to get there. Once again, thanks to [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313)++ ++for the assist.++

Hi [@Lana Laureano](/t5/user/viewprofilepage/user-id/60773) --

Well, for one thing, I believe **[Lookup Issues](https://support.atlassian.com/cloud-automation/docs/jira-automation-actions/#Lookup-issues)** returns **{{lookupIssues}}** as a [list](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/), so I think that what you've written checks that field for *every* issue that was looked up, if it is in fact working at all.

(Because I'm not sure what the expected behavior would be for the [**startsWith**](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/#startsWith-String-str-) function when you are looking at a list instead of a single string.)

If you're confident that only ***one*** issue will ever be returned from your Lookup Issues action, then you might try adding **.first** to limit checking to a single issue's field value:

```
{{#if(not(lookupIssues.first.Create/Update Automated Tests?.value.startsWith("N")))}}
{{lookupIssues.first.Create/Update Automated Tests?.value.startsWith("N")}} {{/}}
```

The other suggestion I'd have is to put the field name in quotation marks, like:

```
{{#if(not(lookupIssues.first."Create/Update Automated Tests?".value.startsWith("N")))}}
{{lookupIssues.first."Create/Update Automated Tests?".value.startsWith("N")}} {{/}}
```

I can't find the post, but [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) discovered the quoting thing a while back. Haven't done any testing recently to see if it's actually still required.

comments: author: Joy Chiu
body: There is the doc for your reference

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

Thanks,

Joy \<Automation for Jira\>


author: Lana Laureano
body: The more general question I haven't been able to understand is how exactly conditional logic against lists is working. If I want to get the sum of "StoryPoints" for all lookup issues where "Create/Update Automated Tests?" is set to "Needed for Release", then what kind of function would I need to write? It seems like adding that check as the if block still results in all the lookup issues existing inside of it.

This is also what I was intending to highlight above - my understanding was that I could filter down the lookupIssues by checking against a condition, but I found that checking against the same condition inside the if block, nothing was filtered out at all.

I would really like to do this without re-getting the lookupIssues with a new JQL query because I'm still using these lookupIssues as part of my field edit as well.


author: Darryl Lee
body: Here's a great article on [Filtering smart value lists.](https://community.atlassian.com/t5/Automation-articles/Filtering-smart-value-lists/ba-p/1827588)

Hmm if you're looking to sum up Story Points up only if ... I wonder if something this would work:

Before the Lookup, set a variable **{{NeededForReleaseStoryPoints}} = 0**

And then after the lookup:

```
{{#lookupIssues}}
{{#if(not("Create/Update Automated Tests?".value.startsWith("N")))}}{{#=}}
{{NeededForReleaseStoryPoints}} + {{Story points}}{{/}}
{{/}}
```

I need to test this.


author: Darryl Lee
body: Side question: Is "Create/Update Automated Tests?" a text field?

I would expect that field to be a required single-select (Yes or No) or a single checkbox. Having to do **.startsWith("N")** is a little... odd.

:-}


author: Lana Laureano
body: It's not a text field, it's a select field with the options Needed, Not Needed, Needed for Release


author: Darryl Lee
body: Huh, interesting. So what are the options you ARE trying to match for?

BTW my code above doesn't work, but I **think** I'm getting closer. Update in a bit.


author: Darryl Lee
body: Hey [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) and [@Sam Harding](/t5/user/viewprofilepage/user-id/3964520) for the life of me I cannot figure out why none of these work. Wondering if you have any thoughts.

The goal is eventually to take the result of FilteredStoryPoints and do a **.split** and **.sum**.

(Where I have a few issues where Single Select="Test")

```
FilteredStoryPoints: {{#lookupIssues}}{{#if(Single Select.value.startsWith("T"))}}{{Story Points}},{{/}}{{/}}

FilteredStoryPoints: {{#lookupIssues}}{{#if(Single Select.startsWith("T"))}}{{Story Points}},{{/}}{{/}}

FilteredStoryPoints: {{#lookupIssues}}{{#if(equals(Single Select, "Test"))}}{{Story Points}},{{/}}{{/}}
```

While this returns all the Story Points and all the Single Selects for each issue:

```
AllStoryPoints: {{#lookupIssues}}{{Story Points}},{{/}}
AllSelects: {{#lookupIssues}}{{Single Select.value}},{{/}}
```

<br />

Here's what I see in the Audit Logs:
> AllStoryPoints: 7.0,2.0,1.0,  
> AllSelects: Test,,Test,  
FilteredStoryPoints:  
(Testing on Cloud.)


author: Lana Laureano
body: I'm not sure why but I tried to respond and my message got blocked with "You may have recently posted the same thing elsewhere on the community. Update your text, or wait before reposting."

Hope this doesn't get me in trouble, but I'm attaching an image of what I typed up - I don't know how else to respond, or how long I'd have to wait before reposting. Thanks for all your help with this [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322)

![Screen Shot 2023-01-24 at 11.59.00 AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/236218i112DCC40753C1A43/image-size/large?v=v2&px=999 "Screen Shot 2023-01-24 at 11.59.00 AM.png")


author: Darryl Lee
body: Hi [@Lana Laureano](/t5/user/viewprofilepage/user-id/60773) - yeah, it's really weird. I can't get any of my filters to work either.

That's interesting that the Manual QA points thing works, although I'm thinking that if **QAPoints - Manual** actually evaluates to null, then wouldn't that evaluate to... null, which might be interpreted to zero?


author: Bill Sheboy
body: Hello, all!

It is possible to filter and sum...but you cannot do that with the built-in lookup issues functions. Instead combine the math operators with filtering and handle the empty cases. For example,

{{#=}} 0 {{#lookupIssues}}{{#if(*some filter expression* )}} + {{*some field*}}{{/}}{{/}}

The leading 0 and the placement of the + {{*some field*}} are key to prevent any null values collapsing the entire result to null, or leading to an error.

Kind regards,  
Bill


author: Darryl Lee
body: Thanks [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) - this would totally be helpful... if I could get the filters to work at all!

As mentioned, I haven't even gotten to the part where I try to add the numbers together. I can't get the numbers to show up at all with these attempts:

```
FilteredStoryPoints: {{#lookupIssues}}{{#if(Single Select.value.startsWith("T"))}}{{Story Points}},{{/}}{{/}}

FilteredStoryPoints: {{#lookupIssues}}{{#if(Single Select.startsWith("T"))}}{{Story Points}},{{/}}{{/}}

FilteredStoryPoints: {{#lookupIssues}}{{#if(equals(Single Select, "Test"))}}{{Story Points}},{{/}}{{/}}
```

I've even tried customfield_10079.value in case the space in the field name was messing things up, as we know it sometimes does. And... nada:

```
FilteredStoryPoints: {{#=}} 0 {{#lookupIssues}}{{#if(equals(customfield_10079.value,"Test"))}} + {{Story Points}}{{/}}{{/}}{{/}}
```

It's the darndest thing.


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

I wonder if this is either a field type/value or a smart value typo thing...I tried this with another single select, custom field's value and it worked for me in Jira Cloud...

total count: {{#=}}0{{#lookupIssues}}{{#if(Current environment.value.startsWith("T"))}} + 1{{/}}{{/}}{{/}}

and total story points: {{#=}}0{{#lookupIssues}}{{#if(Current environment.value.startsWith("T"))}} + {{Story points\|0}}{{/}}{{/}}{{/}}

Of note: I added null handling and changed the capitalization of the "Story point" smart value.


author: Darryl Lee
body: AUGH. I figured out my problem. My "Single Select" field was misnamed (I was reusing it from some other thing) - it was **actually** a Multi Select, so I guess it was returning a list. Bah.

I created a new **actual** "Single Select" field and it worked exactly as you described [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313)!

SO, [@Lana Laureano](/t5/user/viewprofilepage/user-id/60773) as long as "Create/Update Automated Tests?" is a what Jira calls a "Select List (single choice)" field, then this should work for you:

**{{#=}}0{{#lookupIssues}}**   
**{{#if(not(Create/Update Automated Tests?.value.startsWith("N")))}} + {{Story points}}{{/}}{{/}}{{/}}**

(I even tested the field name without quotes, and it works!)

(Hrm, I wonder if there's a way to check if **any** options of a multi-select field start with N.)


author: Darryl Lee
body: Re: checking a multi-select field. It doesn't look like you can iterate a list inside of another list iteration:

**{{#=}}0{{#lookupIssues}}{{#Not Really Single Select}}{{#if(value.startsWith("N"))}} + {{Story Points}}{{/}}{{/}}{{/}}{{/}}**

I thought about using join and then looking for .match(".\*,T.\*"), but that would never match if "Needed" was the first option in the multi-select.

But this is all theoretical, since I'm pretty sure your Lana's field is a single select.


author: Bill Sheboy
body: Yes, and...it is possible to nest iterators but apparently not for the same list (.i.e. same lookup issues result set). A child list (e.g. Sprint, multi-select field, etc.) can be walked inside.

Darryl, for your extended use-case, I would instead suggest pre-filtering to a created variable to create a delimited list of key\|value pairs, where "key" is the list of multi-select field values, and then use match and text functions to get at the "value". This would circumvent the problem of many smart values cannot be used as function parameters.


author: Lana Laureano
body: Thank you so much [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) and [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) for all your help with this!