Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Changing the issue type with groovy

Hi,

I would like to change the issue type from "Bug" to "support" with a groovyscript when a user clicks on a transition "Start Progress". How can I get "this Issue" in a Postfunction? and which objects do I need?

P.S.: both issue types use the same workflow.

Tanks for your help,

Raffy

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted

As stated in comments - you should be sure that issue types share field configuration and at the least workflow states. For details look at implementation of com.atlassian.jira.web.action.issue.MoveIssue* actions.

If you search for scriptrunner examples, you would find out that issue is accessible as 'issue', so

import com.atlassian.jira.component.ComponentAccessor

def newIssueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(issue.projectObject).find{it.name=="support"}
if (newIssueType) issue.setIssueTypeObject(newIssueType)

 

UPDATE 2015-07-15: corrected code as suggested below by @Joe Smith

Thanks Jozef,

I am sure that the workflow and the field configuration scheme are the same.

Hi Jozef,

your solution is pretty much coller than my solution:

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

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

def constantsManager = ComponentAccessor.getConstantsManager()
Issue issue = issue

log.debug ("### Other method ###")
def newIssueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(issue.projectObject).find{it.name=="Task"}
log.debug newIssueType.name
if (newIssueType) issue.setIssueTypeObject(newIssueType)

log.debug issue.name
log.debug("#############################  IssueType changed from 'Bug' to 'Task' ########################################")

But Unfortunately it did not work and I get javax.script.ScriptException (s) in the log file:

Here are the errors:
2013-02-05 11:21:58,859 http-bio-8080-exec-15 ERROR admin 681x857x1 1w8c413 10.255.255.6 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: name for class: com.atlassian.jira.issue.IssueImpl 2013-02-05 11:21:58,859 http-bio-8080-exec-15 ERROR admin 681x857x1 1w8c413 10.255.255.6 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: name for class: com.atlassian.jira.issue.IssueImpl at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:138)

nevertheless thank you very much for your Help.

your error is in line log.debug issue.name

should be log.debug issue.issueType.name

Now it works. Tanks a lot.

jozef kotlár thanks for your example, but there it is actually slightly wrong... if (newIssueType) issue.setIssueObject(newIssueType) should be if (newIssueType) issue.setIssue*Type*Object(newIssueType) as in @dy raffy's example. Got me on the right track though. Thanks both

The solution I was locking for is:

import org.apache.log4j.Category
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
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()

Issue issue = issue  // This what I was looking for :-). As simple as it is, but I didn't know that.

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 == "Support"){
		targetIssueType = issueType
	}
}

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

Why should this be dangerous? It's used deliberately.

How can this be used for moving to another project ? 

Looks a bit dangerous. Btw, it is not just workflow, it is also the Field Configuration Scheme that should be same for both issue types.

Yes, the Field Configuration Scheme is also the same for both issue types.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
Community showcase
Published in Jira

Building a powerful Kanban dashboard in Jira with Great Gadgets app

Kanban is a popular agile framework suitable for both operations and development teams. It does not matter if you are a support team handling issues reported by customers, an internal service desk te...

9 views 0 0
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you