Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy: Changed Issue Type is not saved

Manuel
Contributor
January 13, 2020

Hi,

I'm looking for a a solution to change the issue type from an existing issue by using script runner / groovy.

I've found the following script, but the issue isn't updated:

import org.apache.log4j.Category
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.issuetype.IssueType

// Define a Logger
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.setLevel(org.apache.log4j.Level.DEBUG)

def constantsManager = ComponentAccessor.getConstantsManager()

//Test
//def issueManager = ComponentAccessor.getIssueManager()
//def issue = issueManager.getIssueObject("JIRA-1234")
Issue issue = issue

IssueType targetIssueType = null

log.debug "IssueType old = " + issue.issueType.name

collection = constantsManager.getAllIssueTypeObjects()
iterator = collection.iterator()
while(iterator.hasNext()){
issueType = iterator.next()
if(issueType.name == "Kleinstauftrag"){
targetIssueType = issueType
}
}

log.debug targetIssueType.name
issue.setIssueTypeObject(targetIssueType)
log.debug "IssueType new = " + issue.issueType.name

 

The log shows the new issue type but when i reload the issue it's still the old value. I do not understand why the change is not saved.

 

I'm running the code in the script console.

 

Best regards

Manuel

2 answers

1 accepted

1 vote
Answer accepted
robert Mugabuhamye
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.
January 13, 2020

hello @Manuel .

you need to trigger the update event at the end of you script so that it takes effect.
something like :


issueManager.updateIssue(User, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Manuel
Contributor
January 14, 2020

Hi Robert,

thanks! I will check if i can get it working.

 

Best Regards

Manuel

1 vote
PD Sheehan
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.
January 14, 2020

You're only changing the issue type. If the other issue type has a different workflow, the old workflow will still be associated to the issue. There may also be different required fields for the new issue types.

Changing an issue type in the UI requires going through the whole MOVE operation. Not something very easy to do with scriptrunner.

There are a few people that have attempted it and others have straight up said it couldn't/shouldn't be done.

Here are a few links for you to review:

https://community.atlassian.com/t5/Marketplace-Apps-Integrations/how-to-change-issue-type-and-workflow-programmatically-via/qaq-p/599444

https://community.atlassian.com/t5/Jira-questions/Is-it-possible-to-programatically-move-issue-to-another-project/qaq-p/936099

https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-move-an-issue-programatically-to-a-different-project/qaq-p/515955

Manuel
Contributor
January 15, 2020

Hi Peter,

thanks for that advanced feedback. It's terrible that there is no way by an worklfow to move the issue to another issue type or project - when there is a way in GUI.

In our case we are in same project, use same workflow and field config. So there is no problem when we change the issue type.

Best Regards

Manuel

Nic Brough -Adaptavist-
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.
January 15, 2020

The way in the GUI goes through all the checks and migrations that are necessary when you want to move an issue.

There is a way to do it in code, but you still have to do all the checks that the GUI does before it actually moves the issue to a new type (if you don't, an admin could make a minor change to your project and either stop the script working, or completely break all the issues that are then moved by your code). 

I very briefly looked at scripting a move issue process years ago, but quickly realised that if I was coding to change an issue type or project, I almost certainly had a broken process, not a missing function.  I didn't get very far with it, but I'd start with https://docs.atlassian.com/software/jira/docs/api/8.5.3/com/atlassian/jira/web/action/issue/MoveIssue.html if you still want to do this.

Manuel
Contributor
January 15, 2020

Hi Nic,

Great, i will take a look into this.

Thanks,

Manuel

wiisoftware-corp May 7, 2020

Hello, did you manage to run?

Manuel
Contributor
May 8, 2020

It's depending where you use the script. In the script editor you need to update the issue. In a workflow post function the script is working because there is fired an update to issue by the transition.

Here is our final script  - you must change ISSUEID, ISSUETYPENAME, USERNAME to valid options for your instance.

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.issuetype.IssueType


//If you need to save the change in the ScriptConsole: activate the next line
//import com.atlassian.jira.event.type.EventDispatchOption.EventDispatchOptionImpl

def constantsManager = ComponentAccessor.getConstantsManager()
def cfm = ComponentAccessor.customFieldManager

//If you need to save the change in the ScriptConsole: activate the next lines
//def issueManager = ComponentAccessor.getIssueManager()
//def issue = issueManager.getIssueObject("ISSUEID")

//If you need to save the change in the WORKFLOW: activate the next line
Issue Issue = issue

IssueType targetIssueType = null
def collection = constantsManager.getAllIssueTypeObjects()
def iterator = collection.iterator()
while(iterator.hasNext()){
def issueType = iterator.next()
if(issueType.name == "NEWISSUETYPENAME"){
targetIssueType = issueType
}
}
issue.setIssueTypeObject(targetIssueType)

//If you need to save the change in the ScriptConsole: activate the next line
//issueManager.updateIssue('ADMINUSERNAME', issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Like wiisoftware-corp likes this
wiisoftware-corp May 8, 2020

Thank you so much for your answer!

Sorry for my low knowledge. I am unable to make it work. :(

I put the script in the status change action using Groovy and ScriptRunner and it didn't work.

I tried to make some adjustments according to the errors and it still doesn't work:

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption

// If you need to save the change in the ScriptConsole: activate the next line
import com.atlassian.jira.event.type.EventDispatchOption.EventDispatchOptionImpl

def constantsManager = ComponentAccessor.getConstantsManager ()
def cfm = ComponentAccessor.customFieldManager

// If you need to save the change in the ScriptConsole: activate the next lines
def issueManager = ComponentAccessor.getIssueManager ()
Issue mutableIssue = issueManager.getIssueObject (event.issue.id)
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext (). GetLoggedInUser ();

// If you need to save the change in the WORKFLOW: activate the next line
Issue Issue = issue

IssueType targetIssueType = null
def collection = constantsManager.getAllIssueTypeObjects ()
def iterator = collection.iterator ()
while (iterator.hasNext ()) {
def issueType = iterator.next ()
if (issueType.name == "Incident") {
targetIssueType = issueType
}
}
mutableIssue.setIssueTypeObject (targetIssueType)

// If you need to save the change in the ScriptConsole: activate the next line
issueManager.updateIssue (currentUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false)

 

This works partially for me (it changes the issuetype but has problems with the workflow:

def cf = issue.get ("customfield_14840") as String

if (cf.find ("IS PROBLEM"))
{
  def newIssueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject (issue.projectObject) .find {it.name == "Incidente2222"}
  if (newIssueType) issue.setIssueTypeObject (newIssueType)
}
else
{
  def newIssueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject (issue.projectObject) .find {it.name == "Service request with Eventual approval 22222"}
  if (newIssueType) issue.setIssueTypeObject (newIssueType)
}

Suggest an answer

Log in or Sign up to answer