Variable set in if else-if ends up null
Asked by Derek F:

Hi, I am trying to set due dates based on the Component that is set on the Story (Small, Medium, Large). Note that I copy the Components to the sub-tasks in an attempt to simplify the logic of looking up the Components.

I created an if else-if branch to manage these three conditions, and in each I set a variable to hold the date.

Right after the variable is set, I have a For Parent branch to create sub-tasks and assign due dates...but they are turning up blank.

So I added a test audit log entry to see if the variable existed even just after the if else-if branch ended and it doesn't. It's already null.

I can see in the rule audit log that the condition passes successfully so why is my variable null?

thanks

![rule1snip.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/239899i89275FFF6A10E71F/image-size/large?v=v2&px=999 "rule1snip.PNG")![rule2snip.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/239900iB3590D55C966A630/image-size/large?v=v2&px=999 "rule2snip.PNG")![rule3snip.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/239901i1AB48D7C66BA4AFD/image-size/large?v=v2&px=999 "rule3snip.PNG")


Answers:
author: Mikael Sandberg
body: Is the log action within the if/else block or outside? The screenshots indicate that it is outside of the if/else block and that would explain why the variable is null. A smart value that is created with a if/else block is only available within that block and cannot be accessed outside of it.

comments: author: Derek F
body: Outside. The article where they launched this feature says exactly this:
> This article is for the power users of Jira automation to show you how to use our new 'Create variable' action.
>
> This new action allows you to define your own smart value, which can be used in other actions and conditions in the same rule.

<https://community.atlassian.com/t5/Jira-articles/Automation-for-Jira-Create-variable-New-component/ba-p/1448118>

So to me it makes no sense that it is confined to only the if else block. Maybe they'll broaden in the future.


author: Mikael Sandberg
body: It depends on where you create the variable. If you look in the article they are creating the variable first before using it in the branch. If the variable is created with the branch, you will not be able to access it outside of that branch, that's the difference.


author: Jeret Shuck
body: I agree with Derek here. I have a similar automation where I set a variable, and need to use outside the IF block I created it in.

I also found this: <https://jira.atlassian.com/browse/AUTO-154>

Looks like it's an active feature request.


author: Derek F
body: [@Mikael Sandberg](/t5/user/viewprofilepage/user-id/853579) yes you're right -- but the value of the variable is based on which Component is tagged so I have no choice but to set it in an if/else block.


author: Kelly Arrey
body: I haven't tried this but can you create it outside the if/else block and then set in inside?


author: Mikael Sandberg
body: [@Kelly Arrey](/t5/user/viewprofilepage/user-id/510430) yes, if you create the variable outside of the if/else block it becomes a global variable and can then be used inside the if/else block. Have a look at the article above that Derek linked to.


author: Mikael Sandberg
body: [@Derek F](/t5/user/viewprofilepage/user-id/5091373) You can set the variable to null outside of the if/else block and then use it within to set it to the right value.


author: Derek F
body: I reread that a few times so...I use "create a variable" to create it before the loop and then "create a variable" again to set it to the actual value I want and use it?


author: Mikael Sandberg
body: Correct, you will have to use the create a variable multiple times, but as long as you use the same name it will just update the value for the variable.


author: Derek F
body: Dang, I just created the a gross workaround for this (but it works). When I'm feeling brave I'll optimize it. Thank you