[GROOVY/JQL] How to get to fields in ticket


I try to learn how to work with fields so i wanted to create "handmade" JQL result in table with listed fields but i don't now how to get to these fields. Here is my code:

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.web.bean.PagerFilter
def jqlSearch = "project = JIRA"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
UserUtil userUtil = ComponentAccessor.getUserUtil()
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
def result = "<table style=\"width:100%\">"
SearchService.ParseResult parseResult =  searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
    def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
    issueManager.getIssue(searchResult).each{issue ->
              result += "<tr><th>" + field1 + "</th>" + "<th>" + field2 + "</th>" + "<th>" + field3 + "</th></tr>" //fields are just for example what i want to get
} else {
    log.error("Invalid JQL: " + jqlSearch);

And I know that "issueManager.getIssue(searchResult).each" is wrong but i couldn't find anything similar.
If someone could show me how to at least get one field to this i would be really grateful,


1 answer

1 accepted

0 votes
Accepted answer
Vasiliy Zverev Community Champion Nov 30, 2016

Here is code to get value of custom field with name "field name"

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

CustomField field1 = customFieldManager.getCustomFieldObjectByName("filed name")

return issue.getCustomFieldValue(field1);

Note, that this is only for custom fields. For build-in fields (like issue type, priority) it it requred to use method of Issue class.

That help with field problem but i still don;t know how to use
IssueManager.getIssue(searchResult).each or can you advise me any smiliar method smile?

Hey, @Maciej Olszewski. Take a look at the first example on https://scriptrunner.adaptavist.com/latest/jira/recipes/misc/running-a-jql-query.html. That should give you an idea how to get some JQL results and grab the issue object from them.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()

// edit this query to suit
def query = jqlQueryParser.parseQuery("project = JRA and assignee = currentUser()")

def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

log.debug("Total issues: ${results.total}")

results.getIssues().each {documentIssue ->

    // if you need a mutable issue you can do:
    def issue = issueManager.getIssueObject(documentIssue.id)

    // do something to the issue...

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,782 views 18 21
Read article

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