Unable to search issues in groovy as a service


I have written a below groovy script which is rendering issues in Script Console but not in GroovyService.

It shows Total issues >> 0 as a service

but shows Total issues >> 23 issues in Script Console.

Please correct me if I went somewhere wrong or do I need to modify script ?

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Category
import java.util.List;

def log = Category.getInstance("com.onresolve.jira.groovy.example.IssueIteration")
jqlSearch = "project = Agile"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
UserUtil userUtil = ComponentAccessor.getUserUtil()
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
def List<Issue> issues = null 

SearchService.ParseResult parseResult =  searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) 
    def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
     issues = searchResult.getIssues();
	 log.info "Total issues >> "+searchResult.getTotal()
		issues.each{issue ->
			log.info "Issue is "+issue
 else {
    log.error("Invalid JQL: " + jqlSearch );

1 answer

Resolved by

def searchResult = searchService.search(ComponentAccessor.getUserManager().getUserByName("admin").getDirectoryUser(), parseResult.getQuery(), PagerFilter.getUnlimitedFilter())

Yeah, there's no "logged in user" when a service runs.

That's why I was wondering that script runs in console but not in service :)

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Feb 07, 2019 in Marketplace Apps

A Timeless Love Story

It started as any story starts, on a normal, rainy day.   Admin meets App, and her name was Klok2, and like any first relationship we were both trying to make it work but neither one knew what...

441 views 8 26
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you