I'm trying to sum a custom field through a jql query result using a groovy script, but I get a null pointer order. Can anyone be of help? My code is as follows:
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 = "project = TISFOLIO" //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().getLoggedInUser() def searchService = ComponentAccessor.getComponentOfType(SearchService.class) SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch) def value = 0 def customFieldManager = ComponentAccessor.getCustomFieldManager() def myCustomField = customFieldManager.getCustomFieldObjectByName("customfield_13312") 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
I am not sure what namely caused NullPointerExeption, usually it happens when there is no value for caustom field. I add hull check and refactored your code:
import com.atlassian.jira.bc.issue.search.SearchService import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.search.SearchException import com.atlassian.jira.web.bean.PagerFilter //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().getLoggedInUser() def searchService = ComponentAccessor.getComponentOfType(SearchService.class) SearchService.ParseResult parseResult = searchService.parseQuery(user, "project = TISFOLIO") double value = 0 if (parseResult.isValid()) { try { CustomField myCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("customfield_13312") searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter()) .getIssues().each { if (it.getCustomFieldValue(myCustomField) != null) // value += (double) it.getCustomFieldValue(myCustomField) } } catch (SearchException e) { e.printStackTrace() } } else { log.warn("Invalid query") return null } return value
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.