/
Tutorial on using Closures

Tutorial on using Closures

This tutorial will guide you through writing Groovy scripts using Closures.

On this page:

Looping over a collection to find values that satisfy a condition

This section of the tutorial guides you through writing a Closure 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  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.

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

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.


Looping over a collection to find whether a particular option is selected or not

This section of the tutorial guides you through writing a Closure 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

  1. Go to the Administration icon  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.

    issue.get("Scheduled tasks").any{
      it.value == "Printing"
    }

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.

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

Related content

Use cases for post functions (Legacy)
Use cases for post functions (Legacy)
Read with this
Tutorial on looping over collections
Tutorial on looping over collections
More like this
Calculate and fetch a subset of values of a multi-valued field based on a condition
Calculate and fetch a subset of values of a multi-valued field based on a condition
More like this
Tutorial on performing an action based on the value of a field
Tutorial on performing an action based on the value of a field
More like this
Control structures in Groovy
Control structures in Groovy
More like this