IssueService.IssueResult transition doesn't modify the status of the issue after a specified transition

Rosy Salameh July 7, 2014

Hi Atlassians,

Inside my postfunction, I'm using IssueService.IssueResult transition method in order to transition an issue through workflow.

As specified in the documentation, the issue will be saved and re-indexed and the transition will be made.(https://docs.atlassian.com/jira/6.2.1/com/atlassian/jira/bc/issue/IssueService.html#transition(com.atlassian.crowd.embedded.api.User, com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult) ).

As I understand from that, there is no need to update the issue by code as this method will do it.

In my code, I noticed that the transition is happening (by checking History tab) but the issue is not saved (Status is not modified). I used to put issueManager.updateIssue (kindly ckeck highlighted line in the below code) after it in order to force updating the issue.

My question is: Why implementing IssueService.IssueResult transition is not enough to save the issue after transitioning it?

My code is the following:

TransitionValidationResult transitionValidRes = issueService.validateTransition(authenticationContext.getUser().getDirectoryUser(),issue.getId(),configAction.getId(), issueInputParameters);

log.debug("Current Status of the issue: " + issue.getStatusObject().getName());

log.debug("Action ID: " + configAction.getId());

if (!transitionValidRes.isValid())

{

Iterator<String> ite = transitionValidRes.getErrorCollection().getErrorMessages().iterator();

StringBuilder sb = new StringBuilder();

while (ite.hasNext())

{

sb.append(ite.next());

sb.append("\n");

}

throw new Exception(sb.toString());

}

ErrorCollection transitionErrorCollection = issueService.transition(authenticationContext.getUser().getDirectoryUser(), transitionValidRes).getErrorCollection();

if (transitionErrorCollection.hasAnyErrors())

{

log.error(transitionErrorCollection.toString());

}

else

{

log.debug("New Status of Issue: " + transitionValidRes.getIssue().getStatusObject().getName());

}

issue.setStatusObject(transitionValidRes.getIssue().getStatusObject());

issueManager.updateIssue(authenticationContext.getUser().getDirectoryUser(), issue, EventDispatchOption.ISSUE_UPDATED, false);

Thanks,

Rosy

4 answers

1 vote
Jean-François FORGET October 23, 2018

It works by running transition in a new thread from a listener script.

Thread.start({
// your groovy code to run transition
})

 

If you want to run this at the "issue created" step, you will need to apply an additional tricks to check the workflow is fully initialized before runing your transition:

workflowEntries = ComponentAccessor.getOfBizDelegator().findByAnd("OSWorkflowEntry", FieldMap.build("id", issueObject.getWorkflowId()))
workflowState = workflowEntries[0].get("state")

workflowState must be equal to 1 in order to apply any transition.
You might need to do a "while" loop to wait for the workflow state to change from 0 to 1. (few millisec)

 

0 votes
Timothy Harris May 28, 2015

I have the same question. I have been struggling to get this to work, without success, as a listener for a couple of days now sad

0 votes
Rosy Salameh July 7, 2014

Thanks Nic.

However, when I checked the code inside Fast Track Script Runner, I noticed that the same method is being used: issueService.transition

WorkFlowUtils.groovy:

public static Issue actionIssue(String additionalScript, MutableIssue theIssue, Integer actionId, User user, Map scriptParams = [:]) {

IssueService issueService = ComponentAccessor.getIssueService()

IssueInputParameters issueInputParameters = new IssueInputParametersImpl([:])

scriptParams.put("issueInputParameters", issueInputParameters)

ConditionUtils.doAdditional(additionalScript, theIssue, scriptParams)

issueInputParameters = scriptParams['issueInputParameters'] as IssueInputParameters

def validationResult = issueService.validateTransition(user, theIssue.id, actionId as Integer, issueInputParameters)

ErrorCollection errorCollection = validationResult.errorCollection

if (errorCollection.hasAnyErrors()) {

// We used to carry on because sometimes these seem spurious. More investigation needed.

log.error (errorCollection)

log.error ("Not attmpting transition")

return null

}

errorCollection = issueService.transition(user, validationResult).errorCollection

if (errorCollection.hasAnyErrors()) {

log.error (errorCollection)

}

else {

log.info ("Transitioned issue $theIssue through action \"$actionId\"")

}

// reload issue

theIssue = ComponentAccessor.getIssueManager().getIssueObject(theIssue.id)

Status finalStatus = theIssue.statusObject

log.debug "Final status: ${finalStatus.getName()} (${finalStatus.id})"

theIssue

}

Thanks,

Rosy

0 votes
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.
July 7, 2014

Generally, trying to transition an issue while another transition is still executing does not work.

What you've got there would probably work fine in a listener, but I've never seen it work in a post-function (in fact, had jobs cleaning up the mess made by people trying to do this)

I do know it can be done - the script runner and jjupin plugins appear to be doing a transition in a post-function, but it's really not as simple as just trying to kick off a new transition using the issue service (I know at least one of those plugins is effectively starting the transtion after the first one ends),

Suggest an answer

Log in or Sign up to answer