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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.