Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,365,184
Community Members
 
Community Events
168
Community Groups

scriptrunner - change issue status by ID is not working

I try to bulk change issue status by scriptrunner. Somehow he change the status in my log but finaly not in the issue. Here is my code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Logger
import org.apache.log4j.Level

log.setLevel(Level.INFO)

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService.class)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// create query
def query = jqlQueryParser.parseQuery("issuetype = Sub-Task AND parent = SSD-232")
def results = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
// action on result
results.getResults().each {
result ->
def issue = issueManager.getIssueObject(result.id)
issue.setStatusId("6")
issueManager.updateIssue(user,issue,EventDispatchOption.DO_NOT_DISPATCH,true)
log.info(issue.key+issue.getStatus())
}

 If I run the script log.info(issue.key+issue.getStatus()) shows that every issuestatus is set to 6. But if I check by outcomment 

//issue.setStatusId("6")
//issueManager.updateIssue(user,issue,EventDispatchOption.DO_NOT_DISPATCH,true)

every Issue is back at his old status. What is missing here?

 

1 answer

1 accepted

0 votes
Answer accepted

Status is not a field, it's an indicator of the position of an issue in its workflow.

You can't just set a status on an existing issue, you need to do all the other work associated with changing status as well (if nothing else, the status is held in two different places at the same time, and your setStatus call is only affecting one of them).  Set status is only used when creating new issues (create, import or clone processes)

Your code will need to transition the issues instead, unless you want to write a huge swathe of code that updates the os tables, goes through the post functions, re-indexes and so-on for yourself.

There's a simple example of groovy transition code in https://library.adaptavist.com/entity/transition-issues-returned-from-a-jql-search - you should be able to mostly copy and paste that into your listener or console (not post-function though - you cannot transition an issue when it's in the middle of another transition), and replace the search code with whatever you need to id the issue to transition.

Thanks for kicking me in the right direction: transition is the magic I was looking for:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.workflow.JiraWorkflow
import com.atlassian.jira.workflow.WorkflowManager
import org.apache.log4j.Logger
import org.apache.log4j.Level

log.setLevel(Level.INFO)

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService.class)
def issueManager = ComponentAccessor.getIssueManager()
def issueService = ComponentAccessor.getIssueService()
// USER WITH CORRECT PERMISSIONS NEEDED
def user = ComponentAccessor.userManager.getUserByName("servicedeskagent@company.com")
def workflowManager = ComponentAccessor.getWorkflowManager()

// create query
def query = jqlQueryParser.parseQuery("issuetype = Sub-Task AND parent = SSD-232")
def results = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

// do crazy stuff with results
results.getResults().each {
result ->
def issue = issueManager.getIssueObject(result.id)
def workflow = workflowManager.getWorkflow(issue)
//check possible transition IDs for that issue (outcommended)
//log.info(workflow.getLinkedStep(issue.getStatus()).getActions().id)
//get Validation
def transitionValidationResult = issueService.validateTransition(user,issue.id,151,new IssueInputParametersImpl())
// if valid do transition
if (transitionValidationResult.isValid()){
def transitionResult = issueService.transition(user,transitionValidationResult)
}
}

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events