Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Runner: Why cant I adress the issue?

dD
Contributor
September 29, 2021

I used the following code to get issues via a JQL:

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
import org.apache.log4j.Level

// Set log level to INFO
log.setLevel(Level.INFO)

// The JQL query you want to search with
final jqlSearch = "issuetype = 'SUD Modul' and 'Sommersemester aktiv?' = aktiv"

// Some components
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)

// Parse the query
def parseResult = searchService.parseQuery(user, jqlSearch)
if (!parseResult.valid) {
log.error('Invalid query')
return null
}

try {
// Perform the query to get the issues
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.results
issues.each {
log.info(it.key)
}

issues*.key
} catch (SearchException e) {
e.printStackTrace()
null
}

which works and when I excecute this in the console I also receive an Issue e.g. [SUD-42].

Because I want to extend this script and buit a scheduled job, I need to retrieve informations of that issue. E.g. I need to know the assignee and the value of an customfield, but I cant use codes like:

def assignee = issues.getAssignee() 

The programm then states that issues is undeclared. What code can I use, in order to be able to retrieve information from the issue? 

Best thanks in advances for your answers!!

1 answer

1 vote
Mohamed Benziane
Community Champion
September 29, 2021

Hi,

I'm not sure but your variable issues seems to be a list of all issue retrieve by your JQL. So to get your assignee you need to iterate over your list, like you did it for the issue key and get the assignee.

dD
Contributor
September 29, 2021

Hi @Mohamed Benziane this makes sense, thank you!

Do you know how I could do this for instance? I tried sth but it really didnt work :)

Mohamed Benziane
Community Champion
September 29, 2021

This line :

def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)

will return a searchResult object

Then you will be able to get a list of issue with that:

import com.atlassian.jira.issue.Issue
List<issue>  = results.getIssues()
for(i in issues){
log.warn(i.getAssignee)
}

I can't test it right now but let me know.

Like dD likes this
dD
Contributor
September 29, 2021

I added your code but got an errormessage saying: unexpected token: ">".

klammer zui.GIF

Mohamed Benziane
Community Champion
September 29, 2021

i forgot the name of the variable

list<issue> issues = results.getIssues()

you can also try this

def listIssues = results.getIssues()
dD
Contributor
September 29, 2021

Unfortunately it doesnt work either. I tried to add it below the "{...}" construction and within.

If I add it below, the progamm states, that both, the results and the issue variables are undeclared. When I add it in between, the system says that it cannot find matching method ....SearchResult#getIssues().

Mohamed Benziane
Community Champion
September 29, 2021
dD
Contributor
September 29, 2021

yes you are right, so does this mean I cant use a jqlSearch to get issues?

In principle it has to be possible, to search for issues (via sql) and then write an email to the appropriate assignees

dD
Contributor
September 29, 2021
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
import org.apache.log4j.Level
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.jql.parser.JqlQueryParser

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// edit this query to suit
def query = jqlQueryParser.parseQuery("project = SUD and issuetype = 'SUD Modul' and 'Sommersemester aktiv?' = aktiv")

def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

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

search.results.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...
def issueAssignee = issue.assignee())
}

I also tried this code but it also doesnt work. Same problem: issue is undeclared 

Suggest an answer

Log in or Sign up to answer