Static Loop in Jira Automation
Asked by Jan-Otto Kr?pke:

Hi,

I would like todo a static loop in jira automation.

I figure out, that I can use the smart variable:

{{IF(issue,"TopicA,TopicB","TopicA,TopicB").split(",")}} loop over TopicA and TopicB.

Is there an less hacky way to archive this?

I tried

\> {{"TopicA,TopicB".split(",")}}

but, it wont work.


Answers:
author: Kalyan Sattaluri
body: Hello [@Jan-Otto Kr?pke](/t5/user/viewprofilepage/user-id/5453148) Welcome.

Syntax for[If conditional](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-conditional-logic/) is:

* {{if(smartValue, "value if true", "value if false")}} allows you to add a specific value when the condition is not matched.

So in what you have shared in your post, its not looping over anything. It is always evaluating to false because your smart value (issue) is always evaluating to issue key of rule which triggered it and its typically not of the format "topic A, topic B".. But because your value for "false" is the same, you essentially always pass that If condition. And I am not sure why split is included, but thats for another discussion.

A normal use case of "if" would be something like below:

{{IF(equals(now.jiraDate,"2024-03-14"),"ABC","PQL")}}

if I execute above statement today, I will get "ABC" because today's date is 2024-03-14 and so it passed. if I execute the same statement tomorrow, I will get "PQL" because If statement will evaluate to false. Kinda unnecessarily clumsy example but thats how to use If condition.

Now to your iterate / loop topic, you need to look into [lists](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/) /arrays in the context of Jira and how to work with them based on your use case. If you just want to list them out, you can reference the field directly, like {{issue.fixVersions.name}} and Jira will output the list.

OTOH, if you want to do conditional checks for each, you coudl some thing like below:

{{#issue.fixVersions}}  
{{#if(equals(name,"MY-RELEASE"))}}  
{{releaseDate}}  
{{/}}{{/}}

where we are looping and checking if fixVersion name is "MY-RELEASE", only then print the releaseDate.

Or to take it to extreme example, you could have field with array of lists so that could be something like below:

{{#issue.customfield_ABCD}}  
{{#approvers}}  
{{#if(equals(approverDecision,"declined"))}}  
{{approver.displayName}}  
{{/}}  
{{/}}  
{{/}}

Finally, if you are in Cloud, Jira gives a UI way called Advanced Branching which does For/Each on a list so you can do that instead of smart value way listed above.

So, thats a little overview on the topic. Let us know what do you mean "static loop" cause its not clear from your post.

Hope it helps.

comments: author: Jan-Otto Kr?pke
body: Hey [@Kalyan Sattaluri](/t5/user/viewprofilepage/user-id/5312306)

thanks for the explanation. I understand the if function. We can keep the focus on loop functionality.

You explained that I can loop through smart variables like {{issue.fixVersions.name}}. The case is well documented.

But instead using a smart variables, I would like to loop over a static list of items instead using a issue field as source.


author: Kalyan Sattaluri
body: Hello [@Jan-Otto Kr?pke](/t5/user/viewprofilepage/user-id/5453148)

If you have a smart value variable called {{varMyList}} and you put in TopicA, TopicB as values. (the values in varMyList are text/ string and you need to use split to convert).

If you want to loop over this list, you would do:

{{#varMyList.split(", ")}} \* {{.}} {{/}} -\> Prints \* TopicA \* TopicB

You can do advanced branching from UI on {{varMyList.split(", ")}} and do/achieve the looping logic like above step.

So if your "static" list is first stored in a variable, you can act upon it like you would any field. Maybe thats the piece you are missing. If not, I am lost on what you mean static list.


author: Jan-Otto Kr?pke
body: Thanks you.

Unfortunately I cant mark comments as accepted answers.


Tags: cloud

jira-service-management

jira-service-management-cloud