how to change the state of an issue in jira?

I want to change the state of an issue as soon as it is created (based on a certain condition). I have used the following code to change the state of the issue:

 

JiraWorkflow workflow = ComponentAccessor.getWorkflowManager().getWorkflow(issue);
StepDescriptor stepDescriptor = workflow.getLinkedStep(issue.getStatusObject());
ActionDescriptor clarificationDescriptor=null;
for(int i=0;i<stepDescriptor.getActions().size();i++){
ActionDescriptor actionDescriptor = (ActionDescriptor) stepDescriptor.getActions().get(i);
String actionName = actionDescriptor.getName();
if(actionName.equalsIgnoreCase("Request Clarification")){
clarificationDescriptor = actionDescriptor;
}
}
if(clarificationDescriptor !=null){
User assigneeUser = issue.getAssigneeUser();
IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters parameters = issueService.newIssueInputParameters();
parameters.setRetainExistingValuesWhenParameterNotProvided(true);
IssueService.TransitionValidationResult validationResult = issueService.validateTransition(assigneeUser, issue.getId(), clarificationDescriptor.getId(), parameters);
 if (validationResult.isValid()){
// Do Something
} 
else{
System.out.println(validationResult.getErrorCollection().getErrorMessages());
}


But this gives me the following error: 

It seems that you have tried to perform a workflow operation (Received Clarification) that is not valid for the current state of this issue (INBOX-50). The likely cause is that somebody has changed the issue recently, please look at the issue history for details.

 

I have cross verified that my workflow is correct. Also when I am trying to change the state through curl, I am able to do it. So what is the problem with the above code.

 

3 answers

1 vote

Where are you trying to run this code?  Post function?

I just posted the code above which I am trying to run as soon as the issue is created.

You've missed the point of the question. "as soon as the issue is created" tells me nothing about where you're running it. Is it in a post function? If not, then WHERE is it run?

I am creating the issue manually in jira. I have a plugin which I have uploaded in jira. There as soon as the event is created, I run the above code.

Ok, it's an addon, that's something useful. But, for the third time, *where is it running*? Is it a post-function?

0 votes

Hello there,

 

I believe what Nic is saying is that it'd be easier to use a plugin of some sort in order to do it directly in your workflow. I'm pretty sure that you can use Script runner, for example, to automatically check for the ticket status/fields and decide to where it should go.

Have you attempted something like that before?

 

Not quite, I'm just trying to establish where the code is running, because you can only run code like this in certain places.

Hi @Nic Brough [Adaptavist] - we've been going round/round with this.  We're using script-runner event listener (not post-function) and we think this is a race condition.  We see this other occasions but every time on create events.

//this debug always returns the correct initial status and expected transition id
logger.debug("Issue Status is " + issue.getStatusObject().name + " Transition ID is " + TRANSITION_ID)


IssueService issueService = ComponentAccessor.getIssueService()
IssueInputParameters inputParams = issueService.newIssueInputParameters()
...
inputParams.setComment("Some Comment")

IssueService.TransitionValidationResult transitionValidationResult = issueService.validateTransition(currentUser, issue.id, TRANSITION_ID, inputParams )

ErrorCollection col = transitionValidationResult.getErrorCollection()
if(col.hasAnyErrors()){
	//create events always come here with: "It seems that you have tried to perform..."
    getLog().error(col.getErrorMessages());
}else{
	//create events never reach here
    IssueService.IssueResult issueResult = issueService.transition(currentUser, transitionValidationResult)
}

When we debug, it reports the newly-created issue's initial status correctly so it seems logical that the original transaction still has a lock on the object and the returned error about "a workflow operation (X) that is not valid. The likely cause is that somebody has changed the issue recently..." is, of course, incorrect. The "somebody" is the creator of the issue.

Is there a way to have it simply wait until other transactions are complete?

Fast-Track Postfunctions simply do not work on create-events so event listeners seem to be the only way only we can't get them to work either. sad  Surely there's a way to make initial status programmatically conditional.

Thanks in advance for you help.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published 11 hours ago in Jira Ops

Jira Ops Early Access Program Update #1: Announcing our next feature and a new integration

Thanks for signing up for Jira Ops! I’m Matt Ryall, leader for the Jira Ops product team at Atlassian. Since this is a brand new product, we’ll be delivering improvements quickly and sharing updates...

158 views 0 5
Read article

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