ScriptRunner how to change status of an Issue

Roberto L
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 22, 2017

Hello Community,

I am trying to figure out how I can change the Status of an Issue.

I would like to add a functionality such that when a certain event occurs, the status of an issue is automatically changed from "Open" to "Done".

I tried accomplishing this with the following code but had no success:

//Checks if the issue is an Epic
if(issue.getIssueType().name=="Epic"){

MutableIssue epic = issue
epic.setStatusId("2"); //Read that 2 meant status = "Done"

}

The above code runs, as I had previously put a log() and it displayed relevant information regarding the issue such as the issue Key. But for somereason when I go check my Epic the status continues to be the same it was before I ran my code. 

Could someone provide me with guidance regarding this issue.

I appreciate all the help!

-Roberto

3 answers

2 accepted

8 votes
Answer accepted
Aidan Derossett _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 23, 2017

Hey Roberto,

Here is an example of properly transitioning an issue in a script listener:

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject(event.issue.key)
IssueService issueService = ComponentAccessor.getIssueService()
def actionId = 21 // change this to the step that you want the issues to be transitioned to
def transitionValidationResult
def transitionResult
def customFieldManager = ComponentAccessor.getCustomFieldManager()
log.debug("The issue type is: " + issue.getIssueType().name)

if (issue.getIssueType().name == "Task") {

transitionValidationResult = issueService.validateTransition(currentUser, issue.id, actionId,new IssueInputParametersImpl())

if (transitionValidationResult.isValid()) {
transitionResult = issueService.transition(currentUser, transitionValidationResult)
if (transitionResult.isValid())
{ log.debug("Transitioned issue $issue through action $actionId") }
else
{ log.debug("Transition result is not valid") }
}
else {
log.debug("The transitionValidation is not valid")
}
}

Take a look at that and let me know if you need help getting it to work for your specific case. I've tested this myself and it does work as intended.

Hope that helps!

-Aidan 

Roberto L
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 23, 2017

Hey @Aidan Derossett _Adaptavist_,

I appreciate you taking the time to write such an example for me.

It works perfectly! Thank you so much for that.

@Jonny Carter@Nic Brough -Adaptavist- Thank you both for the guidance on this issue. I really appreciate it.

-Roberto

Michael Schultz October 8, 2019

@Aidan Derossett _Adaptavist_ If you wouldn't mind checking my newly created question?

https://community.atlassian.com/t5/Jira-Core-questions/Transition-Inward-Linked-issues/qaq-p/1198108#M46113

Very similar to this one in nature.

Maha vishnu v February 10, 2020

Hello @Roberto Luces 

 

def actionId = 21 // change this to the step that you want the issues to be transitioned to // How to get this ? 

May i know , how to get this action-id for my transition status , i'm using this code line to get the action id , But i got exception for below code 

//return workflow.getLinkedStep(issue.getStatus()).getActions().id  .

 

And my requirement is , when ever client log defect , automatic transition from OPEN -> Review  should apply with auto comment  .

 

Can you please help on this point ?

Erik Buchholz
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 18, 2020

Dear @Maha vishnu v ,

you find the Transition id in the Text view of the workflow.

Go to

https://<your-jira-instance>/secure/admin/workflows/ListWorkflows.jspa

and view your workflow.

Like Maha vishnu v likes this
2 votes
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 22, 2017

You can't just "set" a status - that only works when you're importing a new issue.  You need to transition the status through the workflow.  Your code will need to trigger the transition between "open" and "done".

Is this transition to be done on the issue you are looking at?  e.g. If an issue is in "new" and moves to "open", then some times you want it to go to "done" immediately.  If so, ScriptRunner has a "fast track transition" function whcih can do that.  If it's more complex (like it's an event on a different issue), that might be a bit more involved, but can still be done.

Roberto L
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 22, 2017

Hi @Nic Brough -Adaptavist-,

Thank you for your your quick response. I looked into Fast Track listener as an option for this but i do not think it can fully achieve the functionality I desire.

Currently in my custom listener script I am listening for issues updated, from there I check if the issue is a story. If it is then I get its parent (the Epic) and all the issue links associated with it. From there I loop through them to figure out if they are in the "Done" status, if they all are then I want to change The status of my Epic to "Done". 

I didn't believe that the Fast Track listener was able to accommodate this. Therefore I wrote the current script. I am missing the last portion which is updating the Epic to "Done". 

I understand now that It cant be simply "set". Could you elaborate more on this "transition". 

-Roberto

Roberto L
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 23, 2017

Hi @Jonny Carter and @Nic Brough -Adaptavist-,

I just developed this transition code, I was hoping you could take a look at it and confirm if it works or not.

I ran it and recieved no error messages in Jira, but it still did not change the status of my Epic from "In progress" to "Done" even though all the above conditions where met.

 


Issue epic = issueManager.getIssueObject("EP-12");

// Get the details of the user executing the workflow transition for the issue change history
WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);

// Update the issue by validating the transition can be completed first before executing it.
workflowTransitionUtil.setIssue(epic);
workflowTransitionUtil.setUserkey("genericuser2103");
workflowTransitionUtil.setAction(1); // not 100% sure how to get the ID of my transitions but when I put 1 it runs, any other number so far gives me errors.
workflowTransitionUtil.validate();
workflowTransitionUtil.progress();

 The above code runs but it doesnt change any status.

I was wondering if you could help me figure this out and if the code is correct but my transition number isnt, could you provide me with insight as to how I can figure out what the number should be to move an issue into the "Done" stage. I am an admin in my system, I think that might help when it comes to looking.

Thank you for your time and expertise!!

-Roberto

1 vote
Jonny Carter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 22, 2017

Hey Roberto!

Instead of trying to create a custom listener, ScriptRunner provides a built-in listener called Fast-track transition an issue. It should be available as a listener option (when creating a new listener) to choose from in your instance. Try using that built-in function instead and see if that works any better!

Let us know how it goes!

Roberto L
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 23, 2017

HI @Jonny Carter

thank you for your your quick response. I looked into Fast Track listener as an option for this but i do not think it can fully achieve the functionality I desire.

currently in my custom listener script I am listening for issues updated, from there I check if the issue is a story. If it is then I get its parent (the Epic) and all the issue links associated with it. From there I loop through them to figure out if they are in the "Done" status, if they all are then I want to change The status of my Epic to "Done". 

I didn't believe that the Fast Track listener was able to accommodate this. Therefore I wrote the current script. I am missing the last portion which is updating the Epic to "Done". 

I understand now that It cant be simply "set". I have heard that you can use transitions to move an issue from a certain status to another one. Would you be able to provide me with some guidance regarding this?

-Roberto

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events