Groovy Script accessing variables outside loops

I am trying to setup a fast-track transition using the Script Runner plugin. It appears, however, that I cannot access nor modify variables declared before the loops inside the loops. Similarly, I cannot access variables from inside the loop outside of it. Does anyone have any suggestions?

My current code:

subtasksclosed = true
parent = issue
if(issue.isSubTask()){
	parent = issue.getParentObject()
	Outer:
	for (subtask in parent.getSubTaskObjects()){
		if (subtask.statusObject.name != "Closed"){
        		subtasksclosed = false
  	              break Outer
        	}
                subtasksclosed = true
	}

} else if(issue.subTasks.size > 0) {
	Outer2:
	for (subtask in issue.getSubTaskObjects()){
		if (subtask.statusObject.name != "Closed"){
        		subtasksclosed = false
  	              break Outer2
        	}
	}
}
subtasksclosed == true && parent.statusObject.id == "10020"

2 answers

This widget could not be displayed.
Henning Tietgens Community Champion Oct 04, 2012

Maybe you could try this code, so you don't have to access variables from "the other side":

parent = issue
if(issue.isSubTask()){
    parent = issue.getParentObject()
}

subtasksclosed = parent.subTaskObjects.any{it.statusObject.name != "Closed"}

subtasksclosed && parent.statusObject.id == "10020"

Henning

Yes, write it using any(<closure>). I don't see any obvious reason why the original code is not working, but if you do it like Henning suggests it will be a lot more succinct and more obvious if there is a bug.

Unfortunately, this didn't work either.

Henning Tietgens Community Champion Oct 04, 2012

Any error messages in the log?

Henning Tietgens Community Champion Oct 04, 2012

I think you should try to add a lot of

log.error issue.dump()

...

log.error parent.dump()

...

lines to your source to get the point where the scripts don't behave like expected.

This widget could not be displayed.

Interestingly, if I run this through the built in "Condition Tester" in the plugin, it works fine.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

179 views 1 3
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you