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")
log.setLevel(org.apache.log4j.Level.DEBUG)

// set the new issue type
IssueType newIssueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(issue.projectObject).find{it.name=="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
workflowManager.migrateIssueToWorkflow(currentIssue,newWorkflow,newStatus)

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 Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

309 views 0 1
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