Original question below but having done further testing is more fundamental
How can I use variables in the String replacement part e.g.
{{webresponse.body.name.replace("CHG","GHC")}} works
but VarOne=CHG and varTwo=GHC
{{webresponse.body.name.replace("{{varOne}}","{{varTwo}}")}} gives syntax errors.
How can I use variables in the replace part
Is is possible to do a string replacement on a smart variable that I have created in an automation script, using variables as the replacement string e.g.
Create Variable varLongStr
"Here is my number one text"
Create Variable varOne
"one"
Create Variable varTwo
"two"
and I want to modify varLongstr using something like
{{varLongStr}}.replace("{{varOne}}","{{varTwo}}")}}
but I can't get the syntax right
Community moderators have prevented the ability to post new answers.
Hi @Rees, Ian
Short answer, please try this:
{{varSource.replace(varOne, varTwo)}}
Where the variables are defined as this:
For more information...
Some rule functions accept variable parameters and some will not. And even for those which work standalone, they may not work in other contexts, such as inside of an iterator. The only way to know for certain is experimentation.
Please also note:
You are using the replace() function, which takes text as parameters. Note in my example, there are no quotation marks in the variable definitions. The function call handles that as variables are text value type. The same is true when storing a regular expression on a variable, such as when using replaceAll() or match(). The key is to escape any reserved characters for the regular expressions.
Kind regards,
Bill
Wow, Thank you for this post. I was not aware of this. Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Bill,
I wasn't sure what needed escaping so I tried all of the following and all failed so I am assuming that replace doesn't work with variable parameters and so will use another method
attempt1 = {{varSource.replace({{varOne}},{{varTwo}})}}
attempt2 = {{varSource.replace("{{varOne}}","{{varTwo}}")}}
attempt3 = {{varSource.replace(\"{{varOne}}\",\"{{varTwo}}\")}}
attempt4 = {{varSource.replace("\{\{varOne\}\}","\{\{varTwo\}\}")}}
attempt5 = {{varSource.replace(\{\{varOne\}\},\{\{varTwo\}\})}}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Rees, Ian please see my earlier example. Once inside of the double-curly brackets, there is no need to add additional ones.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Another comment from the future here:
Normally replace(...) accepts Variables. Here are some cases where I have encountered issues and my steps on fixing them
Log action and/or using {{debug}}...{{/}} are your friends for this. Thinking "this should be the output/input" and KNOWING it specifically because it gets posted in a Log are two VERY different things.
Problem: There CAN be issues when your variable and the action using the variable have different "logical" locations. one example of this would be defining your variable in the "main" function while the "recipient" of the variable is inside a conditional branch.
Possible Workaround: Using a Lookup Table has usually done the Trick for me. It might need some fiddeling around with flatten() and transferring Table Values into Variables for a specific branch but usually i could get the function to properly "read" my inputs by playing around experimenting with this in mind.
Problem: Sometimes the input itself doesn't get read right. This only happens to me when i try to set up Variables inside a conditional branch before any variables have been set up inside the main branch.
A classical case would be:
trigger (any)
>if-branch case 1: Customfield A is empty
>>create Variable
>>Name: myVar, Value: {{one}}
>if-branch case 2: Customfield A is not empty
>>create Variable
>>Name: myVar, Value {{two}}
log Action: {{myVar}}
The Log will (at least in the cases i encountered this issue) be empty.
Workaround: "Initialise" the Variable (possibly like this):
trigger (any)
create Variable
Name: myVar, Value: 1
>if-branch case 1: Customfield A is empty
[...]
log Action: {{myVar}}
Now it will show a value (not only "1" but also the case-sensitive smart values).
Added benefit: you can now check if your variable got set up correctly (myVar different from 1) or if some kind of error occured (myVar equals 1).
Sidenote: LookupObjects do set up a variable so they fix the issue aswell if used before the branch.
The pretty recently added Lookup Table togehter with .get() works especially well for me when it comes to using it inside of functions.
Example fit to your initial case:
1) create lookup table
- Name myTable
- (first Row) Name: first | Value: "{{one}}" <-- make sure this is plain Text
- (second Row) Name: second | Value: "{{two}}" <-- make sure this is plain Text
2) use the lookup table entries in another action
{{varSource.replace(myTable.get(first), myTable.get(second))}}Interestingly Í have encountered cases, where i specifically needed to use the .get() operator, when outside of replace() and similar "variable accepting functions" a casual mytable.first smart obnject would suffice. I guess .get() helps to making the "hi, i want you to use a variable"-part more explicit.
@Bill Sheboy have you had any similar encounters or troubles? I would be interested in your take on those.
@Rees, Ian I hope this helps you and other people encountering the same problem.
Kind regards,
Ludwig
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This topic is now closed as the discussion has become outdated. If you have more questions or want to continue the conversation, feel free to start a new topic. For more details on why we close older threads, check out our Rules of engagement - Atlassian Community .
Thank you for your understanding!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Rees, Ian
Unfortunately, functions dont take variables as inputs. So you cannot use your variable inside replace or match or conditional or any other functions. Hope it helps. Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.