Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to get issue object from search results?

Edited

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 Apr 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

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'?

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

 

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

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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you