How to create a smart variable with conditional value?
Asked by Yuliyan Serbezki:

Is there something similar to a switch statement in Jira Automation?

I would like to have a variable called env which has a different value depending on the status of the ticket. Something like if status equals this then env equals that, but I'm not sure what's the best way to do it. I then want to send a web request where that value will be used in the body.

I've tried defining it inline in the body, something like "env": "{{#if(equals(issue.status.name, "TRIGGER DEV DEPLOYMENT"))}}dev{{/}}{{#if(equals(issue.status.name, "TRIGGER INT DEPLOYMENT"))}}int{{/}}{{#if(equals(issue.status.name, "TRIGGER STG DEPLOYMENT"))}}stg{{/}}{{#if(equals(issue.status.name, "TRIGGER PRODEU DEPLOYMENT"))}}prodeu{{/}}{{#if(equals(issue.status.name, "TRIGGER PRODUS DEPLOYMENT"))}}produs{{/}}", but that didn't work.   

I then tried to create the smart value in a separate variable step with the same value as above - that didn't work either.

I then tried to create a separate if status equals block then assign value step for each of these, but that also didn't work. It didn't work even when I tried to assign the var outside of all if statements. What can I be doing wrong?


Answers:
author: Bill Sheboy
body: Hi [@Yuliyan Serbezki](/t5/user/viewprofilepage/user-id/5571954) -- Welcome to the Atlassian Community!

Rather than using created variables or an if / else structure to solve this, I recommend using **Lookup Tables**. That would be easier to maintain and collapse the rule structure to only a couple of steps, making it much easier to read / understand the rule.

To learn more about Lookup Tables, please see this article:

<https://community.atlassian.com/t5/Automation-articles/Update-Create-lookup-table-action-improvements/ba-p/2427798>

Kind regards,  
Bill

comments: author: Yuliyan Serbezki
body: Thanks Bill! That definitely looks tidier - that's what I was hoping for when I started.


author: Hauke Bruno Wollentin
body: I'm not aware of some kind of a switch statement in Automation, but you could solve this with the IF/ELSE condition which also allows *else ifs* ; inside each *if* you are able to define smart value conditions.

After setting the smart value variable it may be needed to re-fetch the issue data (there is an action for that available) but I'm not 100% sure about this one :)

comments: author: Yuliyan Serbezki
body: Re-fetch the issue data? Maybe that's what I'm missing, since I'm wondering why after all this assignment the variable stays empty. Do you know how that's done?


author: Hauke Bruno Wollentin
body: It is just an action like the others :)

![Screenshot (18).png](https://community.atlassian.com/t5/image/serverpage/image-id/342647i46EA4D5F9ABEF70E/image-size/large?v=v2&px=999 "Screenshot (18).png")


author: Yuliyan Serbezki
body: Thanks, but doesn't seem to be doing the trick. My env variable stays empty:

```
"env": ""
```


author: Hauke Bruno Wollentin
body: I played a bit around and it seems like the variable need to be "defined" front-up too to be accessible after running the if/else blocks.

I came up with something like this, which works for me:

![Screenshot (19).png](https://community.atlassian.com/t5/image/serverpage/image-id/342648i8D71531EB7D17672/image-size/large?v=v2&px=999 "Screenshot (19).png")


author: Yuliyan Serbezki
body: Yes, that looks like it should work and is the structure I'm going for, but for some strange reason env is still empty. Even if the if else assignments don't work you would think that at least the initial value would be picked up.  

The only difference I can see is maybe in your example you just pass {{env}}? And in my case I'm using that in a json so the line is "env": "{{env}}", and maybe it's possible that the substitution doesn't happen within quotes?


author: Hauke Bruno Wollentin
body: I tried this also, seems to work.

But I'm trying this with commenting on issues here, maybe the substitution in the WebRequest action is different :(

![Screenshot (21).png](https://community.atlassian.com/t5/image/serverpage/image-id/342698iBF087179F330D0A8/image-size/large?v=v2&px=999 "Screenshot (21).png")![Screenshot (20).png](https://community.atlassian.com/t5/image/serverpage/image-id/342699iC3079049E5DF2BC4/image-size/large?v=v2&px=999 "Screenshot (20).png")


author: Yuliyan Serbezki
body: It worked! It seems like the validation of the web request field doesn't properly process the smart variable, but in reality the value is assigned correctly. Thank you!


author: Leo Prakash_CodeForMe_
body: Hi [@Yuliyan Serbezki](/t5/user/viewprofilepage/user-id/5571954)

If I'm not wrong you will try to assign env value based on the status instead of fetching it from issue.

if so you can build rule something like below

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/342651i098FCC720585A205/image-size/large?v=v2&px=999 "image.png")![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/342654i4675017A6F9B5C27/image-size/large?v=v2&px=999 "image.png")

After all conditions configured in if-else loop.

you can use the smart variable "{{env}}" that should reflect the value assigned based on condition

Regards,

Leo

comments: author: Yuliyan Serbezki
body: Yes, that looks good and I believe I'm doing more or less the same thing. Only difference is in the last step I use {{env}} in a json body so the line is "env": "{{env}}", and I'm wondering if because of the quotes it doesn't get evaluated?


author: Leo Prakash_CodeForMe_
body: I don't think so the quotes are issue here. I would suggest you to use Log action component at the end and check your rule's audit log see you get expected value in env then pass it to post call you are running


author: Lisa Forstberg
body: Hi [@Yuliyan Serbezki](/t5/user/viewprofilepage/user-id/5571954)

Welcome to the community!

Since I'm not a developer from the start I think I would have approached this a bit simple. I would have tested to create a single select custom field called Env, with the values dev, int, stg, prodeu, produs

(Maybe this data might be interesting for Jira users to see? If not you can hide it from the view screen if its just for automations sake.)

Then in the automation:

1. choose trigger on issue transition
2. add issue field condition on project and statuses mentioned
3. add branches
   1. IF statements per status f ex "Trigger Dev Deployment"
   2. edit issue field - to change the custom field Env to what you need to thr
4. Action webrequest include the value of Env with a smart value {{issue.Env}} in your web request body? Alternatively if you're not allowed to include custom field values, maybe action to store smart value based on {{issue.ENV}} and include the smart value in the body.

something like this..and of course repeat the branching

![Screenshot 2024-08-20 at 08.46.43.png](https://community.atlassian.com/t5/image/serverpage/image-id/342633i391025807CE084C9/image-size/large?v=v2&px=999 "Screenshot 2024-08-20 at 08.46.43.png")

I am however suspecting that there might me more elegant solutions to this coming from the other community experts.

best regards

Lisa

comments: author: Yuliyan Serbezki
body: Hi Lisa, I've tried applying your solution and it didn't work - please correct me if you see something I've missed. When my issue transitions into one of said statuses I create the smart variable env = "initialValue" just to see if that will be used later.

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/342641i99D3C627F4687D1A/image-size/large?v=v2&px=999 "image.png")

I then go on a branch for that value and start a series of if and then assign statements:  
![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/342643iA41AA58EA795AD44/image-size/large?v=v2&px=999 "image.png")

And then in the end I fire the web request. But when testing the web request "{{env}}" gets evaluated to "" which really puzzles me. I would think that even if all these if statement assignments do nothing, I would at least get that initial value that I assigned outside of the branch.


Tags: cloud

jira-cloud