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

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

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 ->
    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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,156 views 13 19
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot