How to get issue object from search results?

Andreas Schwab April 21, 2020

I want to Bulk Edit a custom field in my project Jira Instance which I can't handle via normal Bulk Edit, so I created a Groovy script that does the update for me.  

My 'searchResults.results' look like this

[com.atlassian.jira.issue.search.DocumentWithId@d9949a3e, com.atlassian.jira.issue.search.DocumentWithId@c97e3fd5, ... ]

And for each result I try to 

def issueObj = issueManager.getIssueObject(it.docId) 

However, 'issueObj' is always null. It would also be of use to get the issuekey as string from the search results as : 

def issueObj = issueManager.getIssueObject('JIRA-123') 

seem to work. 

Here my complete script:

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

// issues returned from that JQL will get altered
final String searchQuery = 'project = JIRA'

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def query = jqlQueryParser.parseQuery(searchQuery)
def searchResults = searchProvider.search(SearchQuery.create(query, user), PagerFilter.unlimitedFilter)

log.warn("Total issues: ${searchResults.total}") // log.debug() does not work

// the name of the custom field to alter
final String customFieldName = "Impact on Architecture" // confirmed and valid field name that we want to alter

// the new value to set
final String newValue = "No"

def customField = ComponentAccessor.customFieldManager.customFieldObjects.findByName(customFieldName)
assert customField : "Could not find custom field with name $customFieldName"


searchResults.results.each {


def issueObj = issueManager.getIssueObject(it.docId) // here we try to create the object which is empty

log.warn( 'Debug:' + issueObj ) // issueobject is null

// def key = issueObj.getKey() // something like that could help

def issue = issueManager.getIssueObject('JIRA-123') // this works

log.warn( issue )

customField.updateValue(null, issue, new modifiedValue(issue.getCustomFieldValue(customField), newValue), new DefaultIssueChangeHolder())

}

 

2 answers

1 accepted

2 votes
Answer accepted
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 21, 2020

Hi @Andreas Schwab ,

I did similar script sometime back, this may give you some idea

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level
import org.apache.log4j.Logger

def appUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkManager = ComponentAccessor.getIssueLinkManager()
def searchService = ComponentAccessor.getComponent(SearchService.class)
def issueManager = ComponentAccessor.getIssueManager()
def log = Logger.getLogger("Jira Log")
log.setLevel(Level.DEBUG)


def jqlSearch = "project = JIRA"

SearchService.ParseResult parseResult =  searchService.parseQuery(appUser, jqlSearch)
if (parseResult.isValid()) {
    def searchResult = searchService.search(appUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
    def issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}
    issues.each { issue ->
           //you can use "issue" variable to update your issue
        }
    }
}else{
log.error("Invalid JQL :" + jqlSearch)
}

 Hope this helps

 

BR,

Leo

Andreas Schwab April 21, 2020

Thanks a lot, that helped me also with my 'log.debug( )' issue! 

But on 

 def searchResult = searchService.search(appUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())

I get the error: 

 ERROR [common.UserScriptEndpoint]: Script console script failed: groovy.lang.MissingPropertyException: No such property: issues for class: com.atlassian.jira.issue.search.SearchResults

 
But my results look like 'log.debug( searchResult )': 


DEBUG [Jira Log]: com.atlassian.jira.issue.search.SearchResults@29cb8931

The latter is not telling me much... Is it possible to log the available properties of 'searchResult'?

Andreas Schwab April 21, 2020

Ha got it I had to 

def issues = searchResult.results.collect {issueManager.getIssueObject(it.id)} // right

instead of 

def issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)} // wrong

 

Daniel Varela Santoalla June 12, 2020

This doesn't work with Jira 8 though, as searchResult does not have the "issues" attribute any more...

Ok, I see that someone has suggested to use "results" instead. This does work indeed.

However, I think you will need to use the pagination feature too, as results will only give you results for the first page..

0 votes
Klaus Imfeld January 11, 2022

This does not work in my JIRA (Version 8.13.6) The correct Issuecollector is as follows:

def issues = searchResult.results.collect {issueManager.getIssueObject(it.document.getField("key").stringValue())}

Otherwise, you will end up with the wrong issues or null issues

 

 

 

Suggest an answer

Log in or Sign up to answer