Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

464 views 5 18
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you