Tutorial on using Closures
This tutorial will guide you through writing Groovy scripts using Closures.
On this page:
Looping over a collection to perform a calculation on all values of a multi-select field
This section of the tutorial guides you through writing a closure to perform a calculation on all the values of a multi-select field. Imagine a functional use case where you want to calculate the story points of all the stories under the Epic in a Calculate number type field.
Writing the script
Step 1 - Navigate to the custom field
- Go to the Administration icon and click on it.
- Locate Issues from the menu and click on it.
- Locate Custom fields on the left panel.
- Locate the custom field, "Total Story Points"
- Click on the
Step 2 - Write the script in the editor
Write the following script in the editor.
def totalStoryPoints = 0; issue.stories.each{story -> totalStoryPoints += story.get("Story Points")} return totalStoryPoints
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-2
- Click on
Test
- The following result will be displayed.
Step 4 - View the custom field value on the issue view
- Save the formula.
- Perform a re-index as suggested by Jira
- Go to the issue
GIJ-2
- Verify the value displayed on the custom field.
Looping over a collection to perform a calculation on values that satisfy a condition
This section of the tutorial guides you through writing a closure to perform a calculation on values that satisfy a condition. Imagine a functional use case where you want to calculate and display on the issue the number of released versions for the project the current issue belongs to in a Calculated Number field.
Writing the script
Step 1 - Navigate to the custom field
- Go to the Administration icon and click on it.
- Locate Issues from the menu and click on it.
- Locate Custom fields on the left panel.
- Locate the custom field, "Number of released versions"
- Click on the
Step 2 - Write the script in the editor
Write the following script in
Groovy script
.issue.getAvailableOptions("versions").findAll{ it.isReleased() == true }.size()
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-5
- Click on
Test
- Verify the result.
Step 4 - View the custom field value on the issue view
- Save the formula.
- Perform a re-index as suggested by Jira
- Go to the issue
GIJ-5
- Verify the value displayed on the custom field.
Looping over a collection to perform a calculation only when a particular option is selected
This section of the tutorial guides you through writing a closure to perform a calculation only when a particular option of a checkbox type field is selected. Imagine a functional use case where you want to calculate and display a text field with the text Unreleased if at least one of the Fix Version/s of the current issue is unreleased.
Writing the script
Step 1 - Navigate to the custom field
- Go to the Administration icon and click on it.
- Locate Issues from the menu and click on it.
- Locate Custom fields on the left panel.
- Locate the custom field, "Issue Status"
- Click on the
Step 2 - Write the script in the editor
Write the following script in
Groovy script
.if (issue.get("fixVersions").any{it.isReleased() == false}){ return "Unreleased" }
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-5
- Click on
Test
- Verify the result
Step 4 - View the custom field value on the issue view
- Save the formula.
- Perform a re-index as suggested by Jira
- Go to the issue
GIJ-5
- Verify the value displayed on the custom field.