Script runner; assign a created subtask

I'm curently using script runner to create subtasks for several types of issues and I'm tring to find a way to automaticly assign these subtasks to users that I define in custom fields.

Right now I am manually assigning these which is tedious work.

the following script does something similar to what I need but I don't know how to edit it to make it work for me:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
 
ComponentManager componentManager = ComponentManager.getInstance();
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
IssueManager issueManager = componentManager.getIssueManager();
 
CustomField srcField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "CustomField"}
 
cfwt = issue.getCustomFieldValue(srcField);
 
 
if (cfwt == "CustomFieldValue001"){
            issue.setAssignee(ComponentManager.instance.userUtil.getUserObject('vend001'))
            }
             
else if (cfwt == "CustomFieldValue002"){
            issue.setAssignee(ComponentManager.instance.userUtil.getUserObject('vend002'))
            }
else if (cfwt == "CustomFieldValue003"){
            issue.setAssignee(ComponentManager.instance.userUtil.getUserObject('vend003'))
            }

5 answers

if you want to set assignee from user coustom field then try with this code

CustomField userField= getCustomFieldObjectByName("User Field name"); //change field name here
User userFieldValue=(User)issue.getCustomFieldValue(userField);
issue.setAssignee(userFieldValue)

This is the error I get when trying your code together with imports

Code used:
"
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.UserManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor

CustomField userField= getCustomFieldObjectByName("Team Leader");
User userFieldValue=(User)issue.getCustomFieldValue(userField);
issue.setAssignee(userFieldValue);
"

error output:

2013-08-27 13:53:09,945 ajp-bio-127.0.0.1-8009-exec-146 ERROR jonas.nilsson 833x17983x1 uyjn6y 192.168.1.195 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executi ng post-function javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getCustomFieldObjectByName( ) is applicable for argument types: (java.lang.String) values: [Team Leader] at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)

try with the following code

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.fields.CustomField

  
ComponentManager componentManager = ComponentManager.getInstance();
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
  
CustomField srcField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "CustomField"}  
def cfwt = issue.getCustomFieldValue(srcField);
def cf = customFieldManager.getCustomFieldObjects(issue).find { it.name == "Team Leader" }
def cfValue = issue.getCustomFieldValue(cf)
issue.setAssignee(cfValue);

This does not seem to work either. I cannot see if it's a syntax fault of nonworking code...

error message:


2013-09-02 09:45:07,913 ajp-bio-127.0.0.1-8009-exec-166 ERROR jonas.nilsson 585x22319x1 1lstgh5 192.168.1.195 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
at javax.script.AbstractScriptEngine.eval(Unknown Source)
at javax.script.ScriptEngine$eval.call(Unknown Source)
at com.onresolve.jira.groovy.canned.utils.ConditionUtils.doAdditional(ConditionUtils.groovy:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) <+2>
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.invoke(StaticMetaMethodSite.java:43)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:88)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
at com.onresolve.jira.groovy.canned.workflow.postfunctions.AbstractCloneIssue.doScript(AbstractCloneIssue.groovy:76)
at com.onresolve.jira.groovy.canned.workflow.postfunctions.CreateSubTask.super$2$doScript(CreateSubTask.groovy) <+2>
at java.lang.reflect.Method.invoke(Unknown Source)

Did you change the string "CustomField" in the example code?

Add some logging so you can see what exactly is null...

Thanks for your help Jamie. Please close.

This code can be used to assign a subtask to a person defined in a custom field (in this example the field is named "Team Leader")


import
com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.ModifiedValue import com.atlassian.jira.issue.fields.CustomField ComponentManager componentManager = ComponentManager.getInstance(); CustomFieldManager customFieldManager = componentManager.getCustomFieldManager(); // List all available fields //customFieldManager.getCustomFieldObjects(issue).each {log.debug("Field: " + it.name)} try { CustomField srcField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Team Leader"} // log.debug("srcField: " + srcField) def cfValue = issue.getCustomFieldValue(srcField); // log.debug("cfValue: " + cfValue) issue.setAssignee(cfValue); } catch (Exception e) { log.error(e.message, e) }

Can you mark at least one answer as accepted. It's also nice to vote up and "like comments" for the ones you found helpful.

Hey Guys,

I am new to Plugin/Coding, that being said where do I need paste this code to assign the subtask using an person defined in an custom field in the parent issue. Thanks in advance.

If you're using the "create subtask" built-in script from Script Runner, in the "additional issue actions" block.

Thanks Jamie. One last question using you plugin I believe that you can create multiple subtasks in one go under transition post function. Is there any place where I can refer to step-by-step instruction on how to setup creating more than 2 subtasks ? Thanks in advance.

Simplest thing is just to add multiple post-functions.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

23,776 views 2 7
Join discussion

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