Groovy Script to get a list of issuekey, only gives null results

Angel Hernandez
Contributor
March 1, 2025

Scope: I'm trying to run a jql query (it works in the jql search prompt) using a groovy script; to give me a list of issue keys. And it only gives me null results using the Script Runner Console.

Jira Software 9.12.18 - Jira Service Management 5.12.18

Additionally: I want to filter tickets only created between 12am to 8am. According to Grok it should be something like:

def filteredIssues = issues.findAll { Issue issue -> def createdDateTime = issue.getCreated().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime() def hour = createdDateTime.getHour() return hour >= 0 && hour < 8 // 00:00 to 07:59

 

Full Code here:

 

 

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
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.issue.search.DocumentWithId

final String DS = "Project = \"Data Support\" and text ~ \"pipeline\""

/**
 * Custom JQL function to find issues created between 12 AM and 8 AM daily
 */
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def query = jqlQueryParser.parseQuery(DS)

def searchResults = searchProvider.search(SearchQuery.create(query, user), PagerFilter.unlimitedFilter)
def issues = searchResults.getResults()

def issueKeys = issues.collect { DocumentWithId docWithId ->
    def doc = docWithId.getDocument()
    doc.get("issuekey")
}

issueKeys.each { key ->
    log.info("Issue Key: ${key}")
}

// Return the list of keys
return issueKeys

 

 

3 answers

1 vote
Radek Dostál
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.
March 3, 2025

It's likely not "issuekey", but just "key".

 

Grok should tell you that if you push it hard enough to correct itself, or you could not be using `SearchProvider` which is the worse and more painful way of doing jql search, but then again Grok do what Grok does.

0 votes
Paul _Addonix_
Atlassian Partner
March 3, 2025

You need check field name for issue key. Just print all fields for one found issue:

def doc = docWithId.getDocument()
    log.info(doc.toString())
0 votes
Angel Hernandez
Contributor
March 1, 2025

Groovy Null results.jpg

Suggest an answer

Log in or Sign up to answer