---
title: "Tutorial on looping over collections"
canonical: "https://support.appfire.com/space/JMCF/465830202/Tutorial%20on%20looping%20over%20collections"
format: markdown
---
> Macro (aura-html)

> Macro (excerpt)
> 
> <span style="color: #333333">This tutorial will guide you through writing Groovy scripts that loop over collections.</span>

> ℹ️ The snippets in this tutorial can be further simplified using [Closures](https://appfire.atlassian.net/wiki/spaces/JMWE/pages/461702257), explained in the next tutorial.

**<span style="color: #333333">On this page:</span>**


# Looping over a collection to perform a calculation on all values of a multi-select field

<span style="color: #333333">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.</span>

## Writing the script

### Step 1 - Navigate to the custom field

1. Go to the **Administration** icon > Macro (inline-media-image)

 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 > Macro (inline-media-image)

 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. <span style="color: #333333">Write the following script in the editor.</span>

### Step 3 - <span style="color: #333333">Test your script</span>

1. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test Groovy Script`<span style="color: #333333">.</span>
2. <span style="color: #333333">Input the issue key</span><span style="color: #333333"> </span>`GIJ-2`
3. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test`

### Step 4 - <span style="color: #333333">View the custom field value on the issue view</span>

1. <span style="color: #333333">Save the formula.</span>
2. <span style="color: #333333">Perform a re-index as suggested by Jira</span>
3. <span style="color: #333333">Go to the issue </span>`GIJ-2`
4. <span style="color: #333333">Verify the value on the custom field</span>

# <span style="color: #333333">Looping over a collection to perform a calculation on values that satisfy a condition</span>

<span style="color: #333333">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.</span>

## Writing the script

### Step 1 - Navigate to the custom field

1. Go to the **Administration** icon > Macro (inline-media-image)

 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 > Macro (inline-media-image)

 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. <span style="color: #333333">Write the following script in the editor.</span>

### Step 3 - <span style="color: #333333">Test your script</span>

1. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test Groovy Script`<span style="color: #333333">.</span>
2. <span style="color: #333333">Input the issue key</span><span style="color: #333333"> </span>`GIJ-5`
3. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test`
4. <span style="color: #333333">The following result will be displayed.</span>

### Step 4 - <span style="color: #333333">View the custom field value on the issue view</span>

1. <span style="color: #333333">Save the formula.</span>
2. <span style="color: #333333">Perform a re-index as suggested by Jira</span>
3. <span style="color: #333333">Go to the issue </span>`GIJ-5`
4. <span style="color: #333333">Verify the value displayed on the custom field.</span>

# <span style="color: #333333">Looping over a collection to perform a calculation only when a particular option is selected</span>

<span style="color: #333333">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 </span>**<span style="color: #333333">Unreleased</span>**<span style="color: #333333"> if at least one of the Fix Version/s of the current issue is unreleased.</span>

## Writing the script

### Step 1 - Navigate to the custom field

1. Go to the **Administration** icon > Macro (inline-media-image)

 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 > Macro (inline-media-image)

 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. <span style="color: #333333">Write the following script in</span>`Groovy script`<span style="color: #333333">.</span>

### Step 3 - <span style="color: #333333">Test your script</span>

1. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test Groovy Script`<span style="color: #333333">.</span>
2. <span style="color: #333333">Input the issue key</span><span style="color: #333333"> </span>`GIJ-5`
3. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test`
4. <span style="color: #333333">The following result will be displayed.</span>
5. > Macro (inline-media-image)

### Step 4 - <span style="color: #333333">View the custom field value on the issue view</span>

1. <span style="color: #333333">Save the formula.</span>
2. <span style="color: #333333">Perform a re-index as suggested by Jira</span>
3. <span style="color: #333333">Go to the issue </span>`GIJ-5`
4. <span style="color: #333333">Verify the value displayed on the custom field.</span>

### Next >> [Using Closures](https://appfire.atlassian.net/wiki/spaces/JMWE/pages/462160400)