Using variables in functions in smart values in JIRA for automation purpose

Julien Robert March 5, 2021
Hello,

I'm looking to use jira automation rules in an advanced way using variables in smart value functions.

I saw that a ticket covered this topic some time ago but even though it is marked as resolved I am having trouble applying the syntax.
Here is the ticket : https://codebarrel.atlassian.net/browse/AUT-168

I want to do the same, e.g. :
{{issue.summary.replace("blah", "{{issue.fields.reporter.displayName}}")}}


Is there a way to use variables in smart value functions?

 

3 answers

1 accepted

2 votes
Answer accepted
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 5, 2021

Hi @Julien Robert 

Yes, that works.  If you remove the quotation marks and extra curly braces that would work.  For example:

{{issue.summary.replace("blah", issue.reporter.displayName)}}

Best regards,

Bill

Julien Robert March 8, 2021

Hi @Bill Sheboy 

 

Thanks a lot for your answer.

I tried to combine your solution with a variable added by "Create variable" 

Image 111.png

But I failed to use this variable in the replace method. This didn't return anything using the variable whereas it did return something when I directly used values ​​like fieldChange.fromString and fieldChange.toString (the trigger of my action is a change of a field)

Instead of using a custom variable, I used a way to concat "[" and "]" with another topic (https://community.atlassian.com/t5/Jira-questions/Smart-value-with-concatenate-string-in-string-replace-replaceAll/qaq-p/1363587)

Here is my final code that do what I want to do :

{
"fields": {
"summary": "{{issue.summary.replace(fieldChange.fromString.left(0).concat("[").concat(fieldChange.fromString).concat("]"), fieldChange.toString.left(0).concat("[").concat(fieldChange.toString).concat("]"))}}"
}
}

There is probably some way to make it prettier but I stop my search here! 

 

Thanks again !

Like Bill Sheboy likes this
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 8, 2021

Hi, and I am glad you got that to work!

Regarding Create Variables, they definitely need some investigation and improvements to be a more useful tool for automation rules.  Currently, I have only found them useful to build JSON strings for complex edits and to remove things from lists (e.g. Components).

Best regards,

Bill

Like # people like this
Julien Robert March 9, 2021

Thank you

 

Best regards

Julien

Like John Funk likes this
Boris Zozoulia September 16, 2021

I have discovered a similar issue, where I am unable to use earlier created variables as function arguments in smart values, and the workaround is to use issue entity properties instead:
https://community.atlassian.com/t5/Jira-Service-Management/Re-Pass-in-function-parameters-by-reference-in-smart-val/qaq-p/1806987/comment-id/87452#M87452


For some issue, which has at least 6 comments, automation goes as follows...

Create smart variable:
Variable name: commentNumber
Variable value: 5

Set entity property for issue:
property name: prop_commentNumber
property value (JSON):

{"val": {{commentNumber}} }


Log action:

{{issue.comments.get(issue.properties.prop_commentNumber.val)}}

 
Is it the best approach at this moment, and there is a bug in accessing list elements via smart variable values?

Martin Cleaver
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 30, 2022

Hey Boris,

Did you figure this out?

Thanks

Yatish Madhav
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 6, 2022

Hi

I am trying to get something similar too. Anyone got this working. I am filtering a list of email address (about 3 or 4) and want to filter out only example.com emails and the string example.com is in a variable domain - I would like to do something like:

 

{{field.split(", ").match("([a-zA-Z0-9_.+-]+@{{domain}})")}}
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 13, 2022

Hi @Yatish Madhav 

I just retested this to answer another question: the match() function still cannot accept variables for a regular expression for Jira Cloud. 

And...if you are using Server/Data Center I also doubt this would work as those versions do not have Create Variable with which to make the regular expression dynamic.

Kind regards,
Bill

Yatish Madhav
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 14, 2022

Thanks for the response @Bill Sheboy  - that is a pity. We are on Jira Cloud.

I wonder if anyone else has got this working or found a workaround apart from hardcoding the domain (or anything else) instead of using the variable.

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 14, 2022

After a bit more research, I found a work-around I created a year ago for this problem, bypassing the need to use the match() function:

  • Create variable with your list of clients. Note the trailing comma to separate all company/client pairs.
    • name: varClientIdList
    • smart value:
 CompanyName1|CL1,CompanyName2|CL2,CompanyName3|CL3,
  • Create another variable for your reporter domain
    • name: varReporterEmailDomain
    • smart value:
 {{issue.reporter.emailAddress.substringAfter("@").substringBefore(".")}}
  • Get the client id using text functions to bracket the domain value, storing it in a variable
    • name: varClientId
    • smart value: 
{{varClientIdList.substringAfter(varReporterEmailDomain.concat("|")).substringBefore(",")}}
  • Use an advanced compare condition to confirm {{varClientId}} has a value before using it.
Kevin Christmann October 13, 2023

I can't seem to get the replace to work for me and I can't figure out what I'm doing wrong.

Here is my one variable:
2023-10-13_14-01-46.png

Here is a second variable that contains my Jira Smart value. I put the word REPLACE in there for what I want to replace later.

 

I then try to use the following:
{{theThing.replace("REPLACE",theChoice)}}

Which I would hope would result in

{{issue.properties."proforma.forms.i1".design.questions.1.choices.get(0).label}}

But it does not work.

Anyone know what I'm missing?

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 13, 2023

Hi @Kevin Christmann 

There may be some missing context here...

Would you please post an image of your entire rule, images of the actions which perform that replace, and of the audit log details?  Those may help to explain what you are observing.

Kind regards,
Bill

Kevin Christmann October 13, 2023

@Bill Sheboy sure thing! (I just noticed my one screenshot didn't come through in the previous post.

My rule is very simple right now as I'm just trying to test out this specific replace functionality before I put it into my real purpose.

My rule just...

1. Triggered manually

2. Sets the first variable to 1

3. Sets the 2nd variable as seen
2023-10-13_16-15-41.png
4. Comment on the ticket with my attempt at the replace (so I could see if it worked)
2023-10-13_16-13-27.png

 

And here is the audit log. It makes the comment on the ticket, but only the top, hardcoded line works. The replace does not.

2023-10-13_16-20-56.png

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 13, 2023

If you write {{theThing}} to the audit log, what do you observe?

What is being written to the comment added?

 

Side note: I do not know which version of Jira you are using, but if you are using Jira Cloud and you are on Standard or Free license, consider only writing to the audit log for test rules, rather than writing to issues with comments.  Otherwise that will start counting against your limits next month.  Please look here for more information: https://community.atlassian.com/t5/Automation-articles/Introducing-our-new-packaging-model-for-Jira-Cloud-Automation/ba-p/2446099

Like Tomislav Tobijas likes this
Kevin Christmann October 16, 2023

@Bill Sheboy ah great idea! I never thought of that! I'm on Cloud Premium.

So it appears my issue is the variable {{theThing}} is not getting set. It doesn't like me passing in a smart value.

Is there a way for me to force it to accept a literal string?

 

2.png

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 16, 2023

Progress!

I see what your image is showing to create {{theThing}}.  Please describe what that is meant to contain, as it may be the expression to build it has a problem.

It appears you may be trying to replace text in an issue property using some value.

Kevin Christmann October 16, 2023

@Bill Sheboy if you look at my previous post above under #3, you'll see I am attempting to set that value to a specific smart value. My objective is to replace the word "REPLACE" with a number (whatever the user selects in a previous step).

I realize in my example above I have it hardcoded to 1. But that won't be the case in real life. In actuality, I'm simply trying to dynamically use the Jira smart value you see in the screenshot above under #3.

In order to do that, I'm trying to replace the word "REPLACE" with the correct number. But Jira doesn't seem to like me trying to set the variable to a literal string of a smart value. I assume it's trying to interpret that value (which will return nothing until the replace function works).

So the end result (if the replace worked) should be this:
{{issue.properties."proforma.forms.i1".design.questions.1.choices.get(0).label}}

And if I hardcode that, it works just fine. But I can't get it to be dynamic by passing in the selected number.

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 16, 2023

Because that created variable is empty, my hypothesis is the property is one of those dynamic smart values which can only be evaluated in certain ways.  (I am not using ProForma, so I am guessing a bit here...)

Let's confirm that by writing these to the audit log with separate actions:

form design: {{issue.properties."proforma.forms.i1".design}}
all questions: {{issue.properties."proforma.forms.i1".design.questions}}
question ID 1: {{issue.properties."proforma.forms.i1".design.questions.1}}
all choices: {{issue.properties."proforma.forms.i1".design.questions.1.choices}}
first choice: {{issue.properties."proforma.forms.i1".design.questions.1.choices.get(0)}}
all of it: {{issue.properties."proforma.forms.i1".design.questions.1.choices.get(0).label}}

This should confirm at which point the value can be grabbed into a variable.  After that, we can continue the parsing using text and list functions.

Kevin Christmann October 16, 2023

@Bill Sheboy Here are the results. I cleaned it up in notepad so it was slightly easier to read.

form design: {settings={templateId=1, name=Product Management Request, portal={canSubmit=true}, submit={lock=false, pdf=false}, templateFormUuid=3aa949cf-de0d-4d82-a668-752a0ef9e5fc}, layout=[{version=1, type=doc, content=[{type=extension, attrs={extensionKey=question, extensionType=com.thinktilt.proforma, layout=default, parameters={id=1}, localId=8d849933-8ed1-4e65-bf05-f1bde6a60895}}]}, {version=1, type=doc, content=[{type=extension, attrs={extensionKey=question, extensionType=com.thinktilt.proforma, layout=default, parameters={id=6}, localId=572ec7a3-4954-483c-b922-08f75d4d5079}}]}, {version=1, type=doc, content=[{type=extension, attrs={extensionKey=question, extensionType=com.thinktilt.proforma, layout=default, parameters={id=8}, localId=0d6a8fbc-da6e-412a-a551-dc3e5a663f13}}, {type=extension, attrs={extensionKey=question, extensionType=com.thinktilt.proforma, layout=default, parameters={id=9}, localId=2e33f2b8-bc10-49f8-a802-e8a9a3058b7f}}, {type=extension, attrs={extensionKey=question, extensionType=com.thinktilt.proforma, layout=default, parameters={id=10}, localId=ee748136-cf8b-4fe0-998a-16cdc5d61a49}}, {type=extension, attrs={extensionKey=question, extensionType=com.thinktilt.proforma, layout=default, parameters={id=11}, localId=7f8fb1a4-39ea-4f01-baea-ae22c3d24fd6}}]}, {version=1, type=doc, content=[]}, {version=1, type=doc, content=[{type=extension, attrs={extensionKey=question, extensionType=com.thinktilt.proforma, layout=default, parameters={id=15}, localId=1bddd08d-fc1f-4957-99a3-f2f69522eb23}}, {type=extension, attrs={extensionKey=question, extensionType=com.thinktilt.proforma, layout=default, parameters={id=18}, localId=8121c1cd-c5cc-4591-a6b2-ba6669557833}}, {type=paragraph, content=[]}]}], conditions={5={i={co={cIds={1=[1]}}}, o={sIds=[1]}, t=sh}, 7={i={co={cIds={6=[1]}}}, o={sIds=[2]}, t=sh}, 12={i={co={cIds={6=[2]}}}, o={sIds=[3]}, t=sh}}, sections={1={name=My Application, conditions=[5]}, 2={name=Yes, conditions=[7, 5]}, 3={name=My Application No, conditions=[12, 5]}, 4={name=Always, conditions=[]}}, questions={8={type=ob, label=Client, description=, validation={rq=true}, choices=[{id=f97360e7-cbd8-4076-9dec-22740d1c40f8:42022, label=-DIN - x - TEST - ABC Mortgage}], jiraField=customfield_15065, questionKey=}, 10={type=ts, label=App Username, description=i.e. CXX, validation={rq=false}, choices=[], questionKey=}, 18={type=rt, label=Description, description=, validation={rq=true}, jiraField=description, questionKey=}, 15={type=ts, label=Summary, description=, validation={rq=true}, jiraField=summary, questionKey=}, 11={type=ts, label=Vault Username, description=Username under which docs are uploaded, validation={rq=false}, choices=[], questionKey=}, 9={type=ts, label=Tenant ID, description=Does it differ, validation={rq=false}, choices=[], questionKey=}, 6={type=cd, label=Is this request for a new client?, description=, validation={rq=true}, choices=[{id=1, label=Yes, other=false}, {id=2, label=No, other=false}], questionKey=}, 1={type=cd, label=Type of Request, description=, validation={rq=true}, choices=[{id=1, label=My Application, other=false}, {id=2, label=Other, other=false}], questionKey=}}}

all questions: {8={type=ob, label=Client, description=, validation={rq=true}, choices=[{id=f97360e7-cbd8-4076-9dec-22740d1c40f8:42022, label=-DIN - x - TEST - ABC Mortgage}], jiraField=customfield_15065, questionKey=}, 10={type=ts, label=App Username, description=i.e. CXX, validation={rq=false}, choices=[], questionKey=}, 18={type=rt, label=Description, description=, validation={rq=true}, jiraField=description, questionKey=}, 15={type=ts, label=Summary, description=, validation={rq=true}, jiraField=summary, questionKey=}, 11={type=ts, label=Username, description=Username under which docs are uploaded, validation={rq=false}, choices=[], questionKey=}, 9={type=ts, label=Tenant ID, description=Does it differ?, validation={rq=false}, choices=[], questionKey=}, 6={type=cd, label=Is this request for a new client?, description=, validation={rq=true}, choices=[{id=1, label=Yes, other=false}, {id=2, label=No, other=false}], questionKey=}, 1={type=cd, label=Type of Request, description=, validation={rq=true}, choices=[{id=1, label=My Application, other=false}, {id=2, label=Other, other=false}], questionKey=}}

question ID 1: {type=cd, label=Type of Request, description=, validation={rq=true}, choices=[{id=1, label=My Application, other=false}, {id=2, label=Other, other=false}], questionKey=}

all choices: {id=1, label=My Application, other=false}, {id=2, label=Other, other=false}

first choice: {id=1, label=My Application, other=false}

all of it: My Application

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 16, 2023

Okay, now I think I am following: you are trying to dynamically change which question from ProForma is accessed, based on a variable.

If so, that cannot work the way you have tried it as the rule would need to dynamically interpret the smart value.  Instead you may either try:

  1. create a Lookup Table, using the question ID as the key and the data in the value...and then use your variable for the lookup: {{yourTable.get(theChoice)}}
  2. use the match() function and a dynamic search (with a regular expression) from higher up in {{issue.properties."proforma.forms.i1".design.questions}}

The first way may be easier to understand for maintenance and second way can handle a dynamic number of questions.

Like Kevin Christmann likes this
Kevin Christmann October 16, 2023

@Bill Sheboy wow I think you've done it! I wasn't ware of the new Lookup Table feature! I think this will meet my needs.

Thanks so much Bill! I really appreciate it!

Like Bill Sheboy likes this
Dyyan October 27, 2023

[Resolved] Hi - I'm unable to crack this error, and would appreciate any help/guidance for the fix:

Audit Log view:Screenshot 2023-10-27 at 10.47.17.png

Thanks & Regards,
Dyyan

Tomislav Tobijas
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 27, 2023

If I may jump in here @Dyyan

👀 By the look of the logged error message and the content on the screenshots, aren't you missing one {{/}} in the end (closing of {{#lookupIssues}} smart value)? So basically, just add this string before the "thanks" text.

Like # people like this
Dyyan October 27, 2023

ah, my bad. Thanks so much, @Tomislav Tobijas !

Like Tomislav Tobijas likes this
0 votes
Tomislav Tobijas
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 18, 2023

Hi,

Did anyone manage to get variables to work inside .get() function?
Specifically, we're looking at something like {{lookupObjects.get({{var}})}} where var is the variable.

We've tried all kinds of combinations (with/without quotation marks) but nothing worked.

If this still isn't supported, is there any chance there is some workaround for it?

Cheers,
Tom

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 18, 2023

Hi @Tomislav Tobijas 

There are now at least two versions of get():

  1. The original for lists, which does not accept variables a parameters (You are trying to use that one.)
  2. The get() added for the new Lookup Table action, which is smart enough to handle just about anything passed in.  (That will not help you.)

One possible work-around (that I have not tried recently) is:

  • Iterate over the values in your Lookup Objects, adding the {{index}} as a prefix and a delimiter between records, storing the result in a created variable.  Let's call that varLookupObjects.
  • Create a regular expression to find your object based on the {{var}} you mentioned.  Let's call that varSearchExpression.  Note this separate step is needed as you are creating a dynamic expression for use with the match() function.
  • Split {{varLookupObjects}} on your delimiter (creating a list), and use match(varSearchExpression) to return your record as text
  • Parse the result, as needed, to access the information

It may be possible to do this in just two actions; again, I have not tried that.

Kind regards,
Bill

Tomislav Tobijas
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 19, 2023

Cheers @Bill Sheboy !

I've created a string with a combination of indexes and lookup results - this is actually really cool. Now we just need to create an expression (by using Java pattern) but as we're a bit overbooked I'll leave that for the second phase of integration.

If I manage to build something interesting (or within just two actions), I'll share it here.

Like Bill Sheboy likes this
0 votes
Guido Menardi February 6, 2023

We couldn't use variables inside .match(), then we just use .replace() before and after the use of .match() because replace accepts variables as input.

i.e.:

Using the field description = "a@a.com,b@b.com,2@a.com,c@c.com"

Automation:

  1. Create variable domains = {{#description.split(",")}}{{split("@").get(1)}}{{^last}},{{/}}{{/}}
  2. For each {{domains.split(",").trim().distinct}} as domain
  3. Create variableA = {{description.replace(domain,"temporal_domain")}}
  4. Create variableB = {{#variableA.split(",").match("(.*temporal_domain)")}}{{.}}{{^last}},{{/}}{{/}}
  5. Create variableC = {{variableB.replace("temporal_domain",domain)}}

Results:

  1. domains = [a.com, b.com, a.com, c.com]
  2. domain:
    1. domain(1) = a.com
    2. domain(2) = b.com
    3. domain(3) = c.com
  3. variableA:
    1. variableA(1) = "a@temporal_domain,b@b.com,2@temporal_domain,c@c.com"
    2. variableA(2) = "a@a.com,b@temporal_domain,2@a.com,c@c.com"
    3. variableA(3) = "a@a.com,b@b.com,2@a.com,c@temporal_domain"
  4. variableB:
    1. variableB(1) = "a@temporal_domain,2@temporal_domain"
    2. variableB(2) = "b@temporal_domain"
    3. variableB(3) = "c@temporal_domain"
  5. variableC:
    1. variableC(1) = "a@a.com,2@a.com"
    2. variableC(2) = "b@b.com"
    3. variableC(3) = "c@c.com"
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 6, 2023

Hi @Guido Menardi 

I believe created variables can be used inside the match() function.  The trick is to leave off the outer quotation marks and to let the function assume them around the variable representing the regular expression.

Kind regards,
Bill

hessor November 14, 2023

Can you provide an example of what this looks like? Currently trying to use .match on a webhookresponse body and we need to use variables in the regex. 

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 14, 2023

Hi @hessor -- Welcome to the Atlassian Community!

Which version of Jira are you using?  This technique works for Jira Cloud as it has the Create Variable action.

Here is a simple example, searching for the issue summary in a response message.

  • trigger: some trigger, which provides an issue (e.g., field issue changed)
  • action: send web request, waiting for the response
  • action: create variable
    • variable name: varMatchRegularExpression
    • smart value: .*({{issue.summary}}).*
  • action: log the result of looking for your value
    • here are any matches: {{webResponse.body.match(varMatchRegularExpression)}}

Kind regards,
Bill

hessor November 14, 2023

Oh okay. We have Jira Server so I don't think this would help. Is there any work arounds on Jira Server? We have tried the issue entity property fix but I think that might have also been for Jira Cloud. 

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 14, 2023

I am not using Jira Server, and so cannot test if the match() function will accept a parameter of a smart value.

Before proceeding, I recommend a quick test rule with an example Jira issue: use the Summary as a parameter to search in the Description with match().  If that works, you could try either using a custom field or entity property to hold your search expression.

If it does not work, there may not be a way to do this inside of Server's rules yet.

hessor November 16, 2023

Thank you for your replies. I don't think there is a way to do this on the server side yet. 

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 16, 2023

I just checked and did not find a suggestion for this in the public backlog, and so you may want to add one:

https://jira.atlassian.com/secure/Dashboard.jspa

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