Scriptrunner Jira, getting 0 records with searchService.search

Mehhss May 9, 2024
def masterFilter = "'ISSUETYPE' IN ('PERSON') AND 'STATUS' IN ('ACTIVE, INACTIVE') AND " +
            "PROJECT IN ('Project1') AND 'ID NUMBER' ~ 'ID1234' ORDER BY 'END DATE'  DESC, 'START DATE' DESC";
def parseResults = jqlQueryParser.parseQuery(masterFilter);                      
def masterResults = sServ.search(executeUser, parseResults, PagerFilter.getUnlimitedFilter())
 ;  

log.warn masterResults.total // gives me 0 records 

but when I search with the Query it returns 3 records for ID1234

Is there any mistake in my code for searching the results ?

2 answers

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 10, 2024

Hi @Mehhss

For your requirement, I suggest taking a look and ScriptRunner's HAPI feature to simplify your code.

You could try something like below on the ScriptRunner Console to get the total number of issues returned in the result

Issues.count("""'ISSUETYPE' IN ('PERSON') AND 'STATUS' IN ('ACTIVE, INACTIVE') AND PROJECT IN ('Project1') AND 'ID NUMBER' ~ 'ID1234' ORDER BY 'END DATE'  DESC, 'START DATE' DESC""")

If the result is greater than 0 then you can try to see what is the output returned:-

Issues.search("""'ISSUETYPE' IN ('PERSON') AND 'STATUS' IN ('ACTIVE, INACTIVE') AND PROJECT IN ('Project1') AND 'ID NUMBER' ~ 'ID1234' ORDER BY 'END DATE'  DESC, 'START DATE' DESC""").each { issue ->
log.warn "=====>>> ${issue.key}"

}

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

0 votes
Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 10, 2024

Hi there!

Assuming that your JQL is correct, I can only think that the executeUser does not have permission to view the issues, resulting in 0 results.

Here is what I would do:

  • Try the code with a simpler JQL, one issue (key = xxx)
  • Be sure that the executeUser has access to that issue

 

Here is a sample that worked in my istance, using the loggedIn user, that is yourself when you execute in the Script Console:

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
JqlQueryParser jqlParser = ComponentAccessor.getComponent(JqlQueryParser.class)

String JQL = "key = JIRA-1015"
def query = jqlParser.parseQuery(JQL)
def queryUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def result = searchService.search(queryUser, query, PagerFilter.getUnlimitedFilter())

return result.total

 

Jeroen

 

Suggest an answer

Log in or Sign up to answer