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.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 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"){
		i++
	}
}
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 = searchService.search(user, 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 )
		workflowTransitionUtil.setIssue(missue)
		if (missue.getAssigneeId()!=null) {
			workflowUser = missue.getAssigneeId()
		} else {
			workflowUser = missue.getReporterId()
		}
		workflowTransitionUtil.setUsername(workflowUser)
		workflowTransitionUtil.setAction(transitionId)
		workflowTransitionUtil.progress()
		//re-index issue
		def indexManager = ComponentManager.getInstance().getIndexManager()
		boolean wasIndexing = ImportUtils.isIndexIssues()
		indexManager.reIndex(missue)
		ImportUtils.setIndexIssues(wasIndexing)
		return issue
	}
}
else {
}

1 answer

1 accepted

1 votes

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:

ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(user)

 

 

 

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 context...so that's why I pointed the finger there.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,864 views 12 18
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot