Select Page

Let’s add up the values

Let’s say you have a custom field containing numbers and you want to sum the values of these fields for a set of Jira issues. They can represent revenues from projects, costs on invoices, etc. Your goal is to calculate the sum of these fields from a given set of Jira issues. You could loop over your issues with a for() loop, but there is a nicer way. Using Groovy closures on Jira issues allows for eliminating loops, which shortens the script and increases its readability. You can use the sum() method for your collection of issues instead of iterating with a for() loop over it.

In Groovy, you can call the sum() method on any collection, like so:


This will return the sum of all consecutive integers from 1 to 1000. 1..1000 returns a collection of these integers, and then sum() method on that collection returns the sum of all its elements.
In Jira, we would ideally want to call something like:


Such syntax is incorrect since sum() does not take strings, including custom field names, as arguments. Groovy is unaware of Jira, and it is the script writer’s task to translate the world of Jira into a form understandable by Groovy.

Sum – method’s arguments for Jira custom fields

So what does the sum() method accept as arguments? You can check this here.
The argument type that interests us is Closure. What are closures?

“A closure in Groovy is an open, anonymous, block of code that can take arguments, return a value and be assigned to a variable. A closure may reference variables declared in its surrounding scope” (source)

Yes, we can pass code as an argument to the sum() method, and specify which custom field should be summed.
Like so:

issueList.sum { myCustomField.getValue(it) }

This tells the sum() method to get the value for myCustomField for “it”. “it” is a single element of the collection on which sum() is invoked. In this case “it” is an element of issueList. So “it” is a Jira issue, an object of the MutableIssue class. The sum() method will go over all elements contained in issueList and execute the code between the braces { } to get a value from the custom field. Then, it will return the sum of all values retrieved that way.

Full script

Let’s sum up what we have done. Below is the full script that calculates the sum of custom field values on Jira issues. To simplify, I have hard-coded the keys of two issues for which I wanted to calculate the sum. In reality, you would need to populate the issueList more practically, for example, by executing a JQL query programmatically.

Sum of custom field values on Jira issues - Groovy code for Jira Server and Data Center

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

final NET_PRICE_CF_ID = 11638L // ID of your custom field
final NET_PRICE_CF =

List<MutableIssue> issueList = [

return issueList.sum { NET_PRICE_CF.getValue(it) }