Groovy Script accessing variables outside loops

Cesar Covarrubias October 3, 2012

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

2 votes
Henning Tietgens
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 4, 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

 

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 4, 2012

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.

Cesar Covarrubias October 4, 2012

Unfortunately, this didn't work either.

Henning Tietgens
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 4, 2012

Any error messages in the log?

Henning Tietgens
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 4, 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.

0 votes
Cesar Covarrubias October 3, 2012

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