Lookupissues JQL seems to ignore "If" filter
Asked by Erin Liva:

Hi -

To save reading time I am stating up front that we are using Cloud Team Managed projects and do not have scriptrunner (nor is it likely to be installed in the future).

**Background:** We have created an issuetype 'Requirements' and have subtasks that, once completed, would satisfy the Requirement. Weekly we have to report our progress to the Stakeholders at the Story/Requirement level. Currently we grab the latest subtask due date and total up all the subtask story points into the fields at the Requirement level.

What we are struggling with is the ability to find all the 'done' tasks under the requirement, total the story points up and place that number in a custom field on the Requirement/story level called 'Completed Story Points'. From there we can report the percentage done to align with the graphic at the story/requirement level by doing some calculations.

**Challenge:**

To be safe until fully tested, this is a manually triggered automation at the subtask level. This triggered subtask was done and had 2 story points. Another subtask had 1 story point and was also done. The remaining subtasks (2) were still in other states of progress so should have been filtered out. I therefore expected a result of 3

1. The If filter is already narrowing the issues to subtasks that have a Requirements issuetype as a parent in order to filter out other parents that are stories, tasks, epics etc.)

2. My assumption was that I needed the lookupissues task in order to create a list where I can total up the story points of only the stories that are done so I replicated the JQL which I thought should be the resultset from the If: all match.

3. Once I get that list the total would be stored in a variable for use in the Parent.

The result totaled all story points across the project that were done (147). The log entry of 6 was all story points whether done or not in the triggering subtask.

I have tried many iterations of automation without success and am asking for assistance.

![Automation.png](https://community.atlassian.com/t5/image/serverpage/image-id/227102i14151BD8BB4A7A27/image-dimensions/191x364?v=v2 "Automation.png")![Create Variable.png](https://community.atlassian.com/t5/image/serverpage/image-id/227103i9B83E167B35F42FD/image-dimensions/237x115?v=v2 "Create Variable.png")

![Edit Issue.png](https://community.atlassian.com/t5/image/serverpage/image-id/227104i536E637713691F91/image-dimensions/255x109?v=v2 "Edit Issue.png")

![Audit Result.png](https://community.atlassian.com/t5/image/serverpage/image-id/227105i19D13FCBEE12C95F/image-dimensions/253x165?v=v2 "Audit Result.png")

\[[@William Sheboy](/t5/user/viewprofilepage/user-id/788946) Thank you for directing me to the questions board as I am very new to automation so have limited understanding at this point\]


Answers:
author: Darryl Lee
body: Hi - I think the issue is that variables in Automation are scoped locally, so your **{{TotalStoryPointsDone}}** doesn't even exist within the Parent branch.  

What I would try is to Edit the issue field for the Parent and put in directly: **{{lookupIssues.Story point estimate.sum}}**

comments: author: Darryl Lee
body: This is not quite right. I'm still working on it.

I think the variable thing may be right, BUT ALSO, your JQL query does not scope the results to only subtasks of the PARENT of the current subtask.


author: Darryl Lee
body: WELP, I spent a lot of time on this, and I feel like this should have worked, but it weirdly never makes it to the Parent block (my "DID IT GET HERE?" message never gets logged), so literally have no idea why this isn't working but I suspect it is some Team-managed Project weirdness.

Any thoughts, [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) ?

![Screen Shot 2022-11-22 at 3.53.52 PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/227107i9AD94DA8B40EB5A8/image-size/large?v=v2&px=999 "Screen Shot 2022-11-22 at 3.53.52 PM.png")


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

Erin, sorry I didn't see your mention sooner...It appears you noted my old account that I no longer can access. Darryl mentioned my new one...so here I am :\^)

First thing, I wonder if the problem is the assumption the IF condition impacts the Lookup Issues: it does not.

* The only thing impacting the results of the lookup is the JQL you use. Your lookup JQL doesn't even specify the project...which definitely could be more issues than you intended to gather.
* I suggest testing your JQL in an advanced issue search first to get it working, and then use it in the rule with lookup issues.

Next, please consider posting an image of your complete rule and the audit log details from the execution. The full context may help us see what is happening. Thanks!

Hey, Darryl! My understanding of created variables is that once they are defined, they are "global" from that point forward in the rule...with one exception: branches...

* Branches which could have more than one issue run asynchronously and in parallel. For those, the variable essentially goes out of scope and gets zapped over and over, leading to no predicable outcome. I would expect the value set to a variable *before* the branch to remain unchanged after the branch exits...but as there is no guarantee that such branches finish until the end of the rule...who knows.
* Branches which have one-and-only-one issue (e.g. parent, last created issue, etc.) can see the same created variables from earlier. My understanding is one-issue branches are "unrolled" and run inline.

Kind regards,  
Bill


author: harry
body: informative post it is really helpful for m[e.](https://financeinvestation.com)


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

I can't use **{{lookupIssues.Story point estimate.sum}}** in the Parent because it will automatically sum up all the story points for all the subtasks whether they are done or not.

Based on earlier answers, The IF: statement is not like a programmers version where it reduces and then passes the filtered dataset result so thinking I don't need this and should only use Lookupissues since it ignores what was previously filtered?

Is there a way in lookupissues to say:

Project = "People MDM" AND issuetype = 'subtask' AND status in ("Done", "Cancelled") AND \[Parent matches issuetype = "Requirements"\]?

Then I could possibly get the list of the completed subtasks under that Requirement (Story) and count the story points?


author: Erin Liva
body: [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) Thank you for helping on this! I did include images of the audit results and components in my original post. I am embarrassed to say my previous attempts were much more complex/longer (started at the parent level) but then thought I was overthinking it and trimmed it down to the above.

The rule is restricted to only the one project so I thought reiterating the project name was redundant.... but you are right, caution should prevail. :)


author: Bill Sheboy
body: Erin, reading your question about the Lookup Issues JQL, to get sibling issues of the same parent, try checking the parent. Let's assume the trigger issue is a subtask, and so try adding:

parent = {{triggerIssue.parent}}


author: Erin Liva
body: Yippee! Thank you. That worked :)

![Audit Result 2.png](https://community.atlassian.com/t5/image/serverpage/image-id/227336i1094083E07E89C9B/image-size/large?v=v2&px=999 "Audit Result 2.png")![Audit Result 3.png](https://community.atlassian.com/t5/image/serverpage/image-id/227335i1E1A7AD70863A7B4/image-size/large?v=v2&px=999 "Audit Result 3.png")


author: Bill Sheboy
body: Awesome! I am glad to hear that worked for you.


author: Darryl Lee
body: Hi [@Erin Liva](/t5/user/viewprofilepage/user-id/4645896) so glad you got it working. Would you mind sharing your final rule? For some reason I had problems with the **Parent** related branch, and only was able to get it to work with a **JQL** related branch that seems to do the same thing.

Perhaps it's because I'm using a Manual trigger instead of actually off of a field change. In production you said you're triggering off of your subtask being completed, and in my example I'd be triggering off of tasks being completed.

Anyways I think **{{lookupIssues.Story point estimate.sum}}**will work because the Lookup filters for only tasks that are Done (this is a change from my earlier test rule).

And {{triggerIssue}} might be better to be very explicit about which issue we're talking about.

Here's what seemed to work for me:

![Screen Shot 2022-11-23 at 2.06.00 PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/227369i437BEA9AC445C41E/image-size/large?v=v2&px=999 "Screen Shot 2022-11-23 at 2.06.00 PM.png")


author: Darryl Lee
body: One last point, which is kind of a bummer, is that in Company-managed projects you could make your **Total Story Points** field read-only. I looked to find this in my Team-managed project, and alas, apparently it's an open request.

<https://jira.atlassian.com/browse/JSWCLOUD-21916>


author: Erin Liva
body: [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) it would be great if they gave you an option to make fields (especially custom fields) read-only. Alas I am still waiting for a higher need of being to add other issue types under a story e.g. Test issue type vs. subtask.

As for the final code, do you want the extract of the rule or screen prints (and where you want me to send them).


author: Darryl Lee
body: Hey, [@erin](/t5/user/viewprofilepage/user-id/4071261) - I was just interested in a screenshot of the final rule you came up with, including the logic of what kind of branch you used, with the JQL and/or Lookup, etc. Might be useful or others too.

Hrm, interesting about your need to put Test Issues (or other types) under a Story. I found an add-on that offers this:  

<https://www.adaptavist.com/blog/improve-project-structure-by-adding-new-jira-hierarchy-levels>


author: Erin Liva
body: [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) our company Admins are newbies with Jira. I was surprised that I was the first to use dashboards and automation here. I will ask but not holding my breath.


author: Erin Liva
body: [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) I couldn't find a place to send you a message on the rule. I finally decided to go ahead and post on the closed issue. Can you please let me know if there is a better way to send you things vs. reattaching here?![Sum all subtasks done in Requirement story.png](https://community.atlassian.com/t5/image/serverpage/image-id/242750i27F6644439B5BD98/image-size/large?v=v2&px=999 "Sum all subtasks done in Requirement story.png")


author: Erin Liva
body: The suggestions given successfully solved my issue. Thank you to [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) for helping me with this.