how to change issue type and workflow programmatically via scriptrunner

I am trying to add a workflow transition that allows me to hop workflows and issue types when clicking on certain buttons. Looking at the API's for Issue and Workflow manager it should in theory be possible to do this programatically, but it seems to be failing.

When I run this this code inside of a Script Listener that is fired during a workflow post function, no errors are thrown by this code, I see the green check, it shows it ran without error, also checked the logs and nothing wrong there.

The issue type does change, but the workflow seems to only be partially migrated. The View Workflow button on the issue does show the new workflow, but no workflow buttons are available.

import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.StatusManager
import com.atlassian.jira.workflow.WorkflowManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.workflow.JiraWorkflow
import com.atlassian.jira.issue.status.Status
import com.atlassian.jira.issue.IssueManager
import org.apache.log4j.Category

def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")

// set the new issue type
IssueType newIssueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(issue.projectObject).find{"new issue type"}
log.debug ("newIssueType: " + newIssueType)
if (newIssueType) issue.setIssueTypeObject(newIssueType)

// get the mutable issue
IssueManager issueManager = ComponentAccessor.getIssueManager();
MutableIssue currentIssue = issueManager.getIssueObject(issue.key)
log.debug ("currentIssue: " + currentIssue)

// get the desired workflow: 
WorkflowManager workflowManager = ComponentAccessor.workflowManager
JiraWorkflow newWorkflow = workflowManager.getWorkflow("new workflow")
log.debug ("newWorkflow: " + newWorkflow)

// get the desired status
ConstantsManager constantsManager = (ConstantsManager)ComponentManager.getComponentInstanceOfType(ConstantsManager.class);
Status newStatus = constantsManager.getStatusByName("Begin")
log.debug ("newStatus: " + newStatus)

// now transition

Also when looking backend at the issue, it seems like the issue itself is not picking up the correct workflow as when I run this it reports the ID of the old workflow.

 select * from os_wfentry where id = (select workflow_id from jiraissue where issuenum = 1234 and project = (select id from project where pkey = 'XYZ'));

Is there any other part of the API I can use to make this happen? I have made extensive use of google and found no answers on this. 




1 answer

0 votes

You're not going to be able to do this.  Changing the workflow of an issue is part of another process, not something you should do manually.  You should be changing the issue type, because the configuration for an issue (including its workflow) is determined by the issue type.

Changing an issue type is not trivial.  You need to do that by going through all the steps a "move issue" process goes through.  And note that because it's a move, you cannot do it as part of a workflow on the current issue, because the workflow changes.  (You could change an issue type on another issue in a post-function.  For example on sub-tasks of a parent when the parent goes through a transition)

This makes me question the need for it.  Why do you want to change the issue type? 

Thansk for the response.

We are trying to have simplified workflows that only deal with parts of our business process. So general questions come in, it becomes an actual software issue and needs development, so we wanna throw the issue into another type and workflow so its handled differently without having to do many mouse clicks to move the issue. 

Ok, in JIRA, that's a very poor design for a process.  It's going to take you a lot of coding to set up, and it's going to cause you problems with inconsistencies, and reporting.

Your process for an issue type should be reflected in a single workflow for it.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Feb 07, 2019 in Marketplace Apps

A Timeless Love Story

It started as any story starts, on a normal, rainy day.   Admin meets App, and her name was Klok2, and like any first relationship we were both trying to make it work but neither one knew what...

460 views 8 27
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