ScriptRunner - Script Variables depending on projects

SergioA May 8, 2024

Hi, I have a question regarding the use of Script Variables (from ScriptRunner plugin) depending on the project they are accessed. For instance, I know that if I define a variable calle MY_TEST_VAR I can access it from a script:

def myVar = MY_TEST_VAR

If I define these variables:

MY_TEST_VAR_PRJ000001 = "Value for project PRJ000001"

MY_TEST_VAR_PRJ000002 = "Value for project PRJ000002"

Could them be read in a dynamic way? Something similar to this:

def myVar = MY_TEST_VAR_${issue.project.key}


2 answers

0 votes
SergioA May 8, 2024

I published a reply to Dick's message as it was a reply to my own message and I can't delete it, so I have edited the original message and I will post the reply as soon as it allows me to do it.

0 votes
Dick May 8, 2024

Ciao Sergio,

Smart values come with double curly braces, as per this link for syntax. That might solve your issue. 

SergioA May 9, 2024
Hi Dick! Thanks for your advice, I gave it a try and unfortunately didn't work, I got this error:
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: Script1.MY_TEST_VAR_() is applicable for argument types: (Script1$_run_closure1) values: [Script1$_run_closure1@78f8bea2]

at Source)
BUT, trying to print the project key variable with double curly braces as you suggested throwed an unexpected output:
  "delegate": {
    "binding": {
      "variables": {
"MY_TEST_VAR_PRJ000001": "Value for project PRJ000001",
"MY_TEST_VAR_PRJ000002": "Value for project PRJ000002"
Which made me think that there is actually a variable within the script's context which contains the Script Variables. Apparently binding.variables is what I was looking for, and I'm able now to access the variables in this way:
Or in a dynamic way, depending on the issue's project:
binding.variables["MY_TEST_VAR_" + ${issue.fields.project.key}]
Which works as I expected.
Many thanks for your advice :)

Suggest an answer

Log in or Sign up to answer
Site Admin
AUG Leaders

Atlassian Community Events