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

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

This widget could not be displayed.

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

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
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

213 views 1 3
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you