Scriptrunner return to previous status

I have a global transition that clones an issue,  when the clone is complete I want the workflow to return to where it was.  I seem to be able to get the previous status but when I try to transition to it it fails with:

 

def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("INS-4557")

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def changeHistories = changeHistoryManager.getChangeHistories(issue)

def changeItem = ComponentAccessor.getChangeHistoryManager().getChangeItemsForField(issue, 'status')?.last()
def lastStatus = changeItem.getFromString();

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

def workflow = ComponentAccessor.getWorkflowManager().getWorkflow(issue)
def status=workflow.getLinkedStatusObjects().find{item -> item.name==lastStatus}
def lastStatusId = status.id;


IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
issueInputParameters.setComment("Transition Returned to " + lastStatus)


IssueService.TransitionValidationResult validationResult = issueService.validateTransition(currentUser, issue.id, lastStatusId as Integer, issueInputParameters)
def errorCollection = validationResult.errorCollection
log.debug(errorCollection)
if (! errorCollection.hasAnyErrors()) {
issueService.transition(currentUser, validationResult)
}
else {
// log errors etc
}

 

At the line

issueService.transition(currentUser, validationResult)

I get the error The workflow operation with action id '10875' does not exist in the workflow.

I figure that I need to pass a transition ID not a status ID. 
So I think my question is how do I get the transition ID I need for a given status?

 

 

2 answers

1 accepted

Accepted Answer
2 votes

Just putting my answer here for those that might come across this. 

First I created a custom number field (LastTransitionId).

Then on each transition I added a post function that set this field to the ID of the current transition (you could probably do this in a function but I just hard coded the number in the transition).

The IDs are on the workflow text edit screen here:

31-10-2017 11-37-49.png

Then you can use a script post function like this to rexeecute the transition:

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParameters

def log = Logger.getLogger("daiwa.TransitiontoPrevious")
log.setLevel(Level.DEBUG)

def issueManager = ComponentAccessor.getIssueManager()

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def changeHistories = changeHistoryManager.getChangeHistories(issue)

def changeItem = ComponentAccessor.getChangeHistoryManager().getChangeItemsForField(issue, 'status')?.last()
def lastStatus = changeItem.getFromString();

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_17940")
def lastTransitionId = issue.getCustomFieldValue(cField)

IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();

IssueService.TransitionValidationResult validationResult = issueService.validateTransition(currentUser, issue.id, lastTransitionId as Integer, issueInputParameters)
def errorCollection = validationResult.errorCollection

if (! errorCollection.hasAnyErrors()) {
issueService.transition(currentUser, validationResult)
}
else {
log.error(errorCollection)
}


0 votes

You are right, you need to pass in a transition ID to the update, as you don't just set a Status, you have to transition to it.

Your workflow needs to have the transition "back" to the status you want from the current status. 

Manually, you can just look at the workflow and get the transition ID from that, and then hard code it into the script.

If you want to do this programatically, you'll need code that can look at the workflow and work out the transition from current status to desired (I've never bothered to do this, it's too much fuss, but it is possible)

Thanks Nic,

"Your workflow needs to have the transition "back" to the status you want from the current status. "

As they are all global transitions then I guess they all have one available.

you'll need code that can look at the workflow and work out the transition from current status to desired (I've never bothered to do this, it's too much fuss, but it is possible)

Yeah they want the transition to always return from wherever it was called so I guess that is what I need.


The requirement (Clone the issue, wherever it is in the project),  seems quite a common thing to want to do.  Is a post function the best way to achieve this or should I be looking at some other way of doing it ?

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 11, 2018 in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

2,678 views 5 6
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