Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Excerpt

This tutorial will guide you through writing Groovy scripts that loop over collections.


On this page:

Table of Contents

...

Looping over a collection to find values that satisfy a condition

This section of the tutorial will teach guides you through writing a Groovy script to find 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

  1. Go to the Administration icon Image Added and click on it.
  2. Locate Add-ons from the menu and click on it.
  3. Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
  4. Click on Groovy script tester.

Step 2 - Write the script in the editor

  1. Write the following script in the editor.

    Code Block
    languagegroovy
    linenumberstrue
    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-1
  3. Click on Test
  4. The following result will be displayed.
  5. Image Added

Looping over a collection to find whether a particular

...

option is selected or not

This section of the tutorial will teach guides you through writing a Groovy script loop to find the number of released versions for a projectwhether 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

  1. Go to the Administration icon Image Added and click on it.
  2. Locate Add-ons from the menu and click on it.
  3. Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
  4. Click on Groovy script tester.

Step 2 - Write the script in the editor

  1. Write the following script inGroovy script.

    Code Block
    languagegroovy
    linenumberstrue

...

  1. for(i in issue.get("Scheduled tasks")){
      

...

  1. if(i.

...

  1. value == "Printing"){
        return true
      }
      else{
        return false
      }
    }


Step 3 - Test your script

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


Info

Both these snippets can be further simplified using Closures, explained in the next

...

tutorial.