Sum Number Custom Field (Column) Values of JQL Filter Result Groovy Script

Anere Goodness November 25, 2015

Hi,

I'm trying to sum a number custom field (column) values of JQL filter via groovy script. After I get the list of issues I have no idea how to sum the column/custom field values of the issues returned. Any ideas will be highly appreciated. This is my code

def issueManager = ComponentAccessor.getIssueManager()
def jqlSearch = /My JQL Filter/
JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def SearchService searchService = ComponentAccessor.getComponentOfType(SearchService.class)
def ParseResult parseResult = searchService.parseQuery(authenticationContext.getLoggedInUser(), jqlSearch)

def value = 0
def qtty = 0
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField myCustomField = customFieldManager.getCustomFieldObjectByName("Custom Field Name")

if (parseResult.isValid())
{
    try
    {
        def SearchResults results = searchService.search(authenticationContext.getLoggedInUser(), parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
        def List<Issue> issues = results.getIssues()

        issues.each //I have no idea what to do here
        {
            value += (double) it.getCustomFieldValue(myCustomField)
            qtty = value
        }
    }
    catch (SearchException e)
    {
        e.printStackTrace()
    }
}
else
{
    return null
}

 

it returns

groovy.lang.MissingPropertyException: No such property: myCustomField for class: Script936 at Script936$_run_closure1.doCall(Script936.groovy:48) at Script936.run(Script936.groovy:47)


Any pointers of how to do this will be highly appreciated.

1 answer

1 accepted

1 vote
Answer accepted
Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 26, 2015

Hi Anere,

The thing you were missing was the case that the custom field does not exist for an issue in your list, therefore you should perform a check before 

issues.each {
    if (it.getCustomFieldValue(myCustomField))
        value += (double) it.getCustomFieldValue(myCustomField)
}

Another thing is that you use both def and the class definition (you should choose one of them not both). Apart from that, I don't see any other major issues. If it's ok, I will repost your code with the above corrections. 

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter

def jqlSearch = /My JQL Filter/
//If you are going to use a JIRA version => 7 then you should get the ApplicationUser instead of User, to do that remove the .directoryUser
def user = ComponentAccessor.getJiraAuthenticationContext().getUser().directoryUser
def searchService = ComponentAccessor.getComponentOfType(SearchService.class)
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)

def value = 0
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def myCustomField = customFieldManager.getCustomFieldObjectByName("Number_CF_Name")

if (parseResult.isValid()) {
    try {
        def results = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
        def issues = results.getIssues()
        issues.each {
            if (it.getCustomFieldValue(myCustomField))
                value += (double) it.getCustomFieldValue(myCustomField)
        }
    } catch (SearchException e) {
        e.printStackTrace()
    }
} else {
    log.warn("Invalid query")
    return null
}

return value
Anere Goodness November 26, 2015

Thanks Thanos, It worked.. issues.each { if (it.getCustomFieldValue(myCustomField)) value += (double) it.getCustomFieldValue(myCustomField) } this did the trick.

Suggest an answer

Log in or Sign up to answer