Assigning different values to the same variable with conditions in Jira Automations
Asked by Ylan Muller:

I would like to create a variable in a Jira Automation where, based on the values of a smart value, the variable value changes. For example:

1. Create a variable called "laptop_model"

2. If "department" field value == "Engineering" set "laptop_model" to "X"

3. Else, set "laptop_model" to "Y"

4. Use the value of "laptop_model" to populate a field in a "Create Issue" step later in the flow

Is this possible to do? The issue I am having is that the only action I can seem to take for a variable is to create one - I can't edit the value to be used further down in the flow, at least not based on what I've seen. Thanks!


Answers:
author: Mikael Sandberg
body: Hi [@Ylan Muller](/t5/user/viewprofilepage/user-id/5562201),

Welcome to Atlassian Community!

Yes, this is possible, and what you want to do is create a global smart value that is set outside of the if/else branch, you can then reuse that smart value in the if/else to give it different values. Check out [this article](https://community.atlassian.com/t5/Jira-articles/Automation-for-Jira-Create-variable-New-component/ba-p/1448118) for more information.

comments: author: Ylan Muller
body: Thanks [@Mikael Sandberg](/t5/user/viewprofilepage/user-id/853579) - very cool to know that you can use the Create Variable step to modify the value down a branch. I tested this and it looks like it also persists the output from the branch to use in steps outside of the branch as well, which solves my problem!


author: Bill Sheboy
body: Hi [@Ylan Muller](/t5/user/viewprofilepage/user-id/5562201) -- Welcome to the Atlassian Community!

*Regarding that linked article and create variable scoping, I want to clarify the behavior for branches versus if / else rule structures. This may prevent some rule errors in the future.*

When a variable is re-created with same name, it does replace (i.e., shadow) the value, but only in two cases:

* variable is created before, and then re-created within an if / else condition
* variable is created before, and then re-created within a branch which is on **one-and-only-one issue**

Branches which are on one-and-only-one issue are converted to run inline, as if there was no branching. This is the "trick" that article uses, but which it does not fully describe.

Branches which *could be on more than one issue (or advanced branches)*, run in parallel and asynchronously. And so each created variable within the branch is new, and thrown away when the loop item finishes processing. And because the branch runs asynchronously, it may not be done up until the very last step of the rule; thus it could not be relied upon to have an updated value after the branch.

Kind regards,  
Bill


author: Ylan Muller
body: Hi [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) - thank you for this detail. For my automation purposes I am using two different if/else conditions - one to set the laptopModel variable, and the other that creates tickets that use the output of the laptopModel variable from the first condition. In testing this works well, but good to know that advanced branching on multiple issues would not work for this.


Tags: cloud

jira-cloud