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
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)
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.