This tutorial will guide you through writing Groovy scripts that loop over collections.
The snippets in this tutorial can be further simplified using Closures, explained in the next tutorial.
On this page:
Looping over a collection to find values that satisfy a condition
This section of the tutorial guides you through writing a loop to count the options of a multi-valued field that satisfy a condition. Imagine a functional use case where you want to display the number of released versions for a project.
Writing the script
Step 1 - Navigate to the condition
- Go to the Administration icon and click on it.
- Locate Add-ons from the menu and click on it.
- Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
- Click on Groovy script tester.
Step 2 - Write the script in the editor
Write the following script in the editor.
def count = 0; for(i=0;i<issue.getAvailableOptions("versions").size();i++){ if(issue.getAvailableOptions("versions")[i].isReleased()){ count++ } } count
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-1
- Click on
Test
- The following result will be displayed.
Looping over a collection to find whether a particular option is selected or not
This section of the tutorial guides you through writing a loop to find whether a particular check box of a check box type field is selected or not. Imagine a functional use case where you want check whether Printing has been scheduled in the tasks.
Writing the script
Step 1 - Navigate to the condition
- Go to the Administration icon and click on it.
- Locate Add-ons from the menu and click on it.
- Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
- Click on Groovy script tester.
Step 2 - Write the script in the editor
Write the following script in
Groovy script
.for(i in issue.get("Scheduled tasks")){ if(i.value == "Printing"){ return true } else{ return false } }
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-1
- Click on
Test
- The following result will be displayed.