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

Maciej Olszewski November 30, 2016

Hello,

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,

Cheers,
Maciej

1 answer

1 accepted

0 votes
Answer accepted
Vasiliy Zverev
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 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.

Maciej Olszewski November 30, 2016

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?

Jonny Carter
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 30, 2016

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 ->
    log.debug(documentIssue.key)

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

    // do something to the issue...
    log.debug(issue.summary)
}

Suggest an answer

Log in or Sign up to answer