how to change the state of an issue in jira?

cxpsaml June 25, 2015

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
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.
June 25, 2015

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

cxpsaml June 25, 2015

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

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.
June 25, 2015

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?

cxpsaml June 25, 2015

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.

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.
June 26, 2015

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
HomeAway
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.
July 22, 2015

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.

0 votes
Benito Picarelli
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 26, 2015

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?

 

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.
June 27, 2015

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

Suggest an answer

Log in or Sign up to answer