Get a Value From a Map Variable
This topic demonstrates how to use a Jython Script task in Release to extract a value from a key-value map release variable and store it in another variable.
To get a value from a release variable of type key-value map and use the value in a text field, add a Jython Script task that gets the value and stores it in another variable.
Example scenario
A template contains a variable called ${meals}
of type key-value map and the variable contains:
Key | Value |
---|---|
breakfast | eggs |
lunch | salad |
dinner | ratatouille |
Another variable, called ${mealtime}
, can have the value breakfast
, lunch
, or dinner
. This example will show how to get the value salad
when the ${mealtime}
variable is set to lunch
.
Using a script solution
In a script, you can use the following Python syntax to get the value:
${meals}[$mealtime]
Using this syntax in a text field does not work because variables are replaced literally in tasks instead of being interpreted.
You should add a Jython Script task that will get the value from the key-value map and store it in a new variable called ${mymeal}
.
Add a variable and a script
Go to the template's Variables screen and create a variable called ${mymeal}
. Ensure that you clear the Required and Show on Create Release form options.
You can use ${mymeal}
in tasks without it interfering with the creation of a release.
Add a Jython Script task called "Set mymeal variable" and move it just before the task that will use the ${mymeal}
variable. The template will look like:
In the Jython Script task, you can read and set variables using a Python dictionary called releaseVariables
. This is the script in the "Set mymeal variable" task:
releaseVariables['mymeal'] = ${meals}['${mealtime}']
Configure security settings
Because the script sets a release variable by modifying releaseVariables
, ensure that the release has the required permissions.
In the template, go to the Properties screen and set Run script as user and Password to the user name and password of a user that has write permissions on the release.
Create and start the release
You can now create a release from the template and start it.