Jira Automation - "For each" loop with Multiple "if" statements

Garret Zhang March 9, 2023

Hello,

I ran into an issue with For each loop with if condition. Any help is much appreciated it!

Goal:

I want to run a "for each" loop based off a custom field with multiple selections, and subsequently create tasks based on multiple "if" conditions.

Example:

There are three "impacted systems" (custom field) selected in the source (epic). For each impacted systems, if impacted system = NPE, then create a task in another project. If impacted system != NPE, then create a task in the current project.

Expectation:

Total 3 tasks created:

  • 1 task created in a different project.
  • 2 tasks created in current project.

Actual:

only 1 tasks created in another project.

Current Setup:

Capture.PNG

 

I also want to make sure this setup can scale up, because there will be additional impacted system selected that results in additional tasks created in other projects.

Thanks much!

2 answers

1 accepted

0 votes
Answer accepted
Trudy Claspill
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2023

Hello @Garret Zhang 

The version you have will not work. At the first point where a condition is not met, processing for the current issue in the loop will stop and the loop will proceed with the next issue.

Currently Automation doesn't support using IF/ELSE inside a Branch, which is exactly what you need to do.

I advised another poster with the same type of question to set up multiple branches with each branch containing one condition:

For <your branching information>
If Condition1
Action that goes with Condition1
For <your branching information>
If Condition2
Action that goes with Condition2

I'm not sure if that matches your need for scalable, but it does work.

Garret Zhang March 9, 2023

@Trudy Claspill Thank you.

Is it possible to have multiple does not equal condition? 

For impacted systems that does not equal A, B, C and D, then create tasks (for the other ones)

If this is possible then, I can do multiple for each loop to satisfy certain conditions while other for loop to cover all other conditions.

     Example: System A, B, C, D, E, F

                    for A, create task in project A

                    for B, create task in project B

                    for C, create task in project C

                    for rest, create task in our project.

In reality, we may have 10 out of 50 impacted systems to create a task in another project, while the other 40 will be created in our project.

Trudy Claspill
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2023

If the custom field is a selection list field then I advise you to use an Issue Fields condition rather than a Compare Two Values conditions. You can then use the "is not one of" to specify multiple values that you want to ensure are not present.

Screen Shot 2023-03-09 at 4.05.47 PM.png

Garret Zhang March 9, 2023

this did not work for me :(

 

For the last 3 components, I want to say if system did not = npe/npc, then create tasks for all other system in our project.

 

There were 3 system selected: 1 was created in another project, the other 2 didn't run.

Capture.PNG

Garret Zhang March 9, 2023

Not sure if I am been clear... let me try a different way.

 

For system in (A, B, C, D)

     if System = A

           then create tasks in project A

For system in (A, B, C, D)

     if System = B

           then create tasks in project B

For system in (A, B, C, D)

     ****since system A and B already have tasks created, I want A and B to be excluded and only have C and D to be created in our project.**** 

 

So total 4 tasks created, 2 in project A and B, the other two is in our project.

Trudy Claspill
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2023

I see that you are using a For Each Smart Value, and I have just realized this is likely related to another post from you that I answered previously.

https://community.atlassian.com/t5/Jira-Software-questions/Automation-for-JIRA-Create-multiple-stories-based-on-values-of-a/qaq-p/2292036

Since you are using a smart value and branching on the values in a field, my original advice to use an Issue Fields conditions is completely incorrect. Sorry about that.

Either before or after your For Branches that check if the value matches a single explicit value, what you will need to do is have a simple Issue Fields condition to check the original field

For <your branching information>
If Condition1
Action that goes with Condition1
For <your branching information>
If Condition2
Action that goes with Condition2
IF: field issues condition
<original field> (not the Smart Value used in the branched)
is not one of
<the multiple values you specified>
THEN: Action: Create issue
Garret Zhang March 9, 2023

You caught me...

I am very new at this so I'll have plenty of questions (some surely will be dumb/obvious) in the future and I really appreciate the support! :)

 

I tried your suggestion and it did not work. the last "if" condition does not loop, so how would the automation create two additional tasks?

 

For system in (A, B, C, D)

     if System = A

           then create tasks in project A

For system in (A, B, C, D)

     if System = B

           then create tasks in project B

If system != (A, B)

     then create tasks

 

I don't see how this last line will loop through (A, B, C, D) and eliminate A and B. Then create 2 tasks of C and D in our project.

 

Capture.PNG

Trudy Claspill
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2023

Thank you for providing the additional detail. I had not correctly understood your use case.

You're right, my last suggestion isn't correct for getting an issue created for each value in the field that is not one of the previous values.

I'll have to think on this a bit more.

Like Garret Zhang likes this
Garret Zhang March 10, 2023

Hi @Trudy Claspill - Do you have any update regarding this?

Trudy Claspill
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 10, 2023

No, not yet. 

Unfortunately responding to community posts is not my regular job. Sometimes I wish it was so I could spend all my time working on creative solutions. :)

I may not have time to dedicate to this today, but I will look at it over the weekend in that case.

Garret Zhang March 10, 2023

Got it, no problem. Thank you!

Garret Zhang March 13, 2023

Hi @Trudy Claspill - hope you had a great weekend!

I thought about this more, is it possible to set a new variable by removing certain "impacted systems" from the list? something like this:

Capture.PNG

I'll need to remove multiple "impacted systems". After removal, I'll want to use this new variable to run the for loop? I couldn't get this to work though, let me know what you think.

Appreciated it!

Garret Zhang March 13, 2023

Hi @Trudy Claspill - I came up with a way to resolve this in short to mid term.

I created two separate custom field, one for issues that will be created in our project. one for issues that will be created for another project.

This way, I'll run two for loop, and no duplicated issues will be created.

Thank you for your support anyways! :D

0 votes
Daniel Mora
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 18, 2023

@Garret Zhang I think this is the Jira suggestion you will want to +1 and watch
https://jira.atlassian.com/browse/AUTO-67

Garret Zhang April 18, 2023

@Daniel Mora - Added +1 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events