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

1 votes
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.

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

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,855 views 12 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot