Script works in console, but not as a service

I've written this script and it works great in the console, but it's not working when I schedule it as a service. I've tested some other scripts in services and they've all worked. I'm almost certain my problem lies with my method of JQL query, but I don't understand why it works in the console if it's a problem.

I'd love any feedback. Thanks.

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
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 com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl
import com.atlassian.jira.util.JiraUtils
import com.atlassian.jira.util.ImportUtils
// --------------------------------
// 		adjustable variables
// --------------------------------
// project or category
def project = "(project = SAND)"
// issue status to transition from
def status = "(status = Resolved)"
// date field to measure against
def dateField = "resolved"
// the length of time (in days) that should pass
// before the issue auto-closes. only enter the
// number of weekdays, i.e. 5 days for a work-week
// 0 days will mean it will look for older than now
def dayLength = 0
// workflow's transition ID number
def transitionId = 701
// --------------------------------
// 		date and query formation
// --------------------------------
def today = new Date()
def i = 0
while (i<dayLength) {
	today = today.minus(1)
	strDay = today.format("EEEE")
	if (strDay!="Saturday"&&strDay!="Sunday"){
def finalDate = "\"" + today.format("yyyy/MM/dd HH:mm") + "\""
jqlSearch = project + " AND " + status + " AND " + "(" + dateField + " < " + finalDate + ")"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def User user = ComponentAccessor.getUserUtil().getUserByName("n9948469").getDirectoryUser()
// --------------------------------
// 		creation of issue list
// --------------------------------
def List<Issue> issues = null
SearchService.ParseResult parseResult =  searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
	def searchResult =, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
	issues = searchResult.getIssues()
	issues.each{issue ->
		strissue = issue.getKey()
		def ComponentManager componentManager = ComponentManager.getInstance()
		IssueManager issueManager = componentManager.getIssueManager()
		def MutableIssue missue = (MutableIssue) issueManager.getIssueObject(strissue)
		WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class )
		if (missue.getAssigneeId()!=null) {
			workflowUser = missue.getAssigneeId()
		} else {
			workflowUser = missue.getReporterId()
		//re-index issue
		def indexManager = ComponentManager.getInstance().getIndexManager()
		boolean wasIndexing = ImportUtils.isIndexIssues()
		return issue
else {

1 answer

1 accepted

What happens when run as a service, just no issues retrieved? Or transition fails?

Some methods check the current logged in user, just for kicks put this in and see if that works:





That did it. Jamie, you're great!

No worries but... curious to know why it doesn't work without that as you're passing a user object to the relevant methods.

Yeah, it didn't make any sense to me. I even went out of my way to set it to a certain user rather than getting the logged in user, figuring that would work in console but how could it in the service. Every aspect of the script, sans the jql query/iterating through results, worked fine as a service without any sort of logged in user that's why I pointed the finger there.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,723 views 17 21
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