Script runner; assign a created subtask

Cinnober Admins August 26, 2013

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

0 votes
Rathna May 8, 2014

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.

JamieA
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.
May 8, 2014

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

Rathna May 8, 2014

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.

JamieA
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.
May 11, 2014

Simplest thing is just to add multiple post-functions.

0 votes
Cinnober Admins September 17, 2013
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) }
JamieA
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.
September 17, 2013

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.

0 votes
Cinnober Admins September 11, 2013

Thanks for your help Jamie. Please close.

0 votes
RambanamP
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.
August 26, 2013

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);

Cinnober Admins August 29, 2013

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

Cinnober Admins September 1, 2013

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)

JamieA
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.
September 4, 2013

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

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

0 votes
RambanamP
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.
August 26, 2013

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)

Cinnober Admins August 26, 2013
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)

Suggest an answer

Log in or Sign up to answer