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

  1. Go to the Administration icon  and click on it.
  2. Locate Issues from the menu and click on it.
  3. Locate Custom fields on the left panel.
  4. Locate the custom field, "Total Story Points"
  5. Click on the  of the custom field
  6. Locate Configure and click on it.
  7. Click on Edit Groovy Formula

Step 2 - Write the script in the editor

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

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-2
  3. Click on Test
  4. The following result will be displayed.

Step 4 - View the custom field value on the issue view

  1. Save the formula.
  2. Perform a re-index as suggested by Jira
  3. Go to the issue GIJ-2
  4. 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

  1. Go to the Administration icon  and click on it.
  2. Locate Issues from the menu and click on it.
  3. Locate Custom fields on the left panel.
  4. Locate the custom field, "Number of released versions"
  5. Click on the  of the custom field
  6. Locate Configure and click on it.
  7. Click on Edit Groovy Formula

Step 2 - Write the script in the editor

  1. Write the following script inGroovy script.

    issue.getAvailableOptions("versions").findAll{
      it.isReleased() == true
    }.size()

Step 3 - Test your script

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-5
  3. Click on Test
  4. Verify the result.

Step 4 - View the custom field value on the issue view

  1. Save the formula.
  2. Perform a re-index as suggested by Jira
  3. Go to the issue GIJ-5
  4. 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

  1. Go to the Administration icon  and click on it.
  2. Locate Issues from the menu and click on it.
  3. Locate Custom fields on the left panel.
  4. Locate the custom field, "Issue Status"
  5. Click on the  of the custom field
  6. Locate Configure and click on it.
  7. Click on Edit Groovy Formula

Step 2 - Write the script in the editor

  1. Write the following script inGroovy script.

    if (issue.get("fixVersions").any{it.isReleased() == false}){
      return "Unreleased"
    }

Step 3 - Test your script

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-5
  3. Click on Test
  4. Verify the result

Step 4 - View the custom field value on the issue view

  1. Save the formula.
  2. Perform a re-index as suggested by Jira
  3. Go to the issue GIJ-5
  4. Verify the value displayed on the custom field.

Next >> Perform an action based on the value of a field