Tutorial on looping over collections

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 perform a calculation on all values of a multi-select field

This section of the tutorial guides you through writing a loop 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;
    if (issue.issuetype.name != "Epic")
    return totalStoryPoints;
    for(i=0;i<issue.stories.size();i++){
      totalStoryPoints += issue.stories[i].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

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 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 loop 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 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

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-5
  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-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 loop 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.

    def pass = true
    for(i in issue.get("fixVersions")){
      if(i.isReleased() == "false"){
        pass = false
      }
    }
    return pass

Step 3 - Test your script

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-5
  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-5
  4. Verify the value displayed on the custom field.

Next >> Using Closures