Forums

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

Script Runner - create sub-task post function - set component and copy field value from parent

Micah Figone September 5, 2013

I am working on a workflow for employee terminations and would like to do the following:

Issue contains:

  • Summary
  • Description
  • User to be terminated
  • Component

When the parent ticket is created the workflow will create a sub-task for each action that needs to be performed for that user. Each sub-task will be assigned to the appropriate person to handle the action. I am planning on using components for this purpose and make the component lead be the person that is responsible for the item.

I would like to use the Script Runner post function that creates sub-tasks and set the following but I am not familar with the scripting needed to do the following:

  • Summary: User to be terminated - Some default text
  • Compoennt: specified in script

Any help would be greatly appreciated.

5 answers

1 accepted

0 votes
Answer accepted
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.
September 5, 2013

try with this code by changing as per youur requirment

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.fields.CustomField
import com.atlassian.crowd.embedded.api.User

 
MutableIssue issue = issue
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
//to set user picker custom field
CustomField customField = customFieldManager.getCustomFieldObjectByName("User to be terminated")
User user=ComponentManager.getInstance().getUserUtil().getUser('User Name')
issue.setCustomFieldValue(customField, user)
//set components
Project project = issue.getProjectObject()
ProjectComponent component = componentManager.getProjectComponentManager().findByComponentName(project.getId(), "MyComponent")
issue.setComponents([component.getGenericValue()]) 

//set description
issue.setDescription("description");

don't forget to share if you get anything!!

Micah Figone September 5, 2013

I am getting the following error when I use this.

Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script5.groovy: 17: unable to resolve class Project 
 @ line 17, column 9.
   Project project = issue.getProjectObject()
           ^

Script5.groovy: 18: unable to resolve class ProjectComponent 
 @ line 18, column 18.
   ProjectComponent component = componentManager.getProjectComponentManager().findByComponentName(project.getId(), "Google Account")
                    ^

2 errors

Thanks again for your help.

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

add this two imports

import com.atlassian.jira.project.Project;
import com.atlassian.jira.bc.project.component.ProjectComponent;

Micah Figone September 5, 2013

Same problem:

2013-09-06 11:13:22,728 http-bio-8080-exec-11 ERROR blah@uber.com 673x17451x1 1tskddq 127.0.0.1 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script8.groovy: 17: unable to resolve class Project 
 @ line 17, column 9.
   Project project = issue.getProjectObject()
           ^

Script8.groovy: 18: unable to resolve class ProjectComponent 
 @ line 18, column 18.
   ProjectComponent component = componentManager.getProjectComponentManager().findByComponentName(project.getId(), "Google Account")
                    ^

2 errors

Micah Figone September 6, 2013

So after playing around for a while with this I ended up just using the component part of your code as I did not realize that @jamie's post function actually clones the custom field over anyways....

Thank you for your help.

Akhila Arsan May 16, 2014

great! thanks

0 votes
Darly Senecal-Baptiste
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 16, 2013

@Micah:

In order to assign the subtask based on the component lead, you need to set the project default assignee to the project/component lead. After this, you're good on the code that I give you

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.fields.CustomField
import com.atlassian.crowd.embedded.api.User
com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.project.Project;
import com.atlassian.jira.bc.project.component.ProjectComponent;
 

def componentManager = ComponentManager.getInstance();
def componentManager = ComponentManager.getInstance();
def customFieldManager = componentManager.getCustomFieldManager();
def issueFactory = componentManager.getIssueFactory();
def jiraAuthenticationContext = componentManager.getJiraAuthenticationContext();
def issueManager = componentManager.getIssueManager();
def indexManager = componentManager.getIndexManager()


def public CreateSubtaskIssue (MutableIssue parentIssue, ProjectComponent newcomp)
{
   //Creates Subtask
   MutableIssue newSubtask = issueFactory.getIssue();  

   //Gather the User to be terminated field and its value from the parent ticket
   def cfUtbt = customFieldManager.getCustomFieldObjectByName("User to be terminated");
   def cfUtbtV = parentIssue.getCustomFieldValue(cfUtbtV) as User; // Taken from the parent ticket.

   //Here you can do a if statement in order to check whether the field value from the parent is empty or not

   def String username = cfUtbtV.getDisplayName(); // It will display name like Joe Doe or Jenny Jane
   
   // Subtask Summary  Setting --->format: User to be terminated - Default Text

   newSubtask.summary = username + ": " + "Some default format";

   //Wanna copy description from parent? No problem!!!
   newSubtask.description = parentIssue.description;

   //Set the component to the subtask. Did you say one component per subtask?
   def comps = [newcomp] as Collection <ProjectComponent>;
   newSubtask.componentObjects = comps;

   /*Set the subtasks assignee to the Component Owner.
    Note:  At the project admin,set the project's default assignee to the project lead */

   //Load and link the subtask to the parent ticket

   Map<String,Object> newIssueParams = ["issue":newSubtask] as Map<String,Object>;
   def currentUser = jiraAuthenticationContext.getUser() as User;
   GenericValue newSubtaskGV = issueManager.createIssue(currentUser,newIssueParams);

    //reindexing
    indexManager.reIndex(newSubtaskGV);


    //link the subtask with the parent issue
    subTaskManager.createSubTaskIssueLink(parentIssue, newSubtask, componentManager.getJiraAuthenticationContext().getUser());
}



//At the parent ticket

// Gather the current list of components set in the issue ticket.
def componentList = issue.getComponentsObjects() as Collection<ProjectComponent>


if (componentList != null )
{
  for(comp in componentList)
  {
   Create_Subtask(issue, component); //Create a subtask based and one component of the parent ticket goes to the subtask
  }

}


0 votes
Micah Figone September 16, 2013

@Darly yes it is.

0 votes
Darly Senecal-Baptiste
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 16, 2013

@Micah: Is the field User to be terminated is editable at the create issue?

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.
September 5, 2013
Micah Figone September 5, 2013

As I mentioned above I am already trying to use exactly that method. I am having trouble with the code that is needed in the additional issue actions section of the post function.

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 5, 2013

What have you tried so far? Should be straightforward. But if you don't provide anything you have tried, it looks like you are asking people to do your work for you.

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

what is the field type of this "User to be terminated"?

Micah Figone September 5, 2013

@Jamie: As I mentioned I do not know coding very well... so i have not really tried much. I looked around on the internet a little and tried using the script that was mentioned here:

https://answers.atlassian.com/questions/92882/set-component-in-groovy-script

but got the following in the logs:

2013-09-06 09:11:53,307 http-bio-8080-exec-23 ERROR blah@uber.com 551x16053x1 1oi0ks4 199.116.73.34,127.0.0.1 /secure/CreateIssueDetails.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: componentManager for class: Script3
Caused by: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: componentManager for class: Script3
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:318)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:111)
	... 222 more
Caused by: groovy.lang.MissingPropertyException: No such property: componentManager for class: Script3
	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
	at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
	at Script3.run(Script3.groovy:16)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:315)
	... 223 more

@Rambanam: The field will be a user picker.

Micah Figone September 6, 2013

@Rambanam: So I have spent some more time poking around and and looking at a lot of other examples of code to do things that are similar to what i am asking and have ended up with the following.... but I get a NPE.

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.fields.CustomField
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.project.Project 
import com.atlassian.jira.bc.project.component.ProjectComponent

  
MutableIssue issue = issue
parent = issue.getParentObject()

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
//to set user picker custom field
CustomField customField = customFieldManager.getCustomFieldObjectByName("Affected User")
Object affectedUser = customField.getValue(parent)
issue.setCustomFieldValue(customField, affectedUser)
//set components
Project project = issue.getProjectObject()
ProjectComponent component = componentManager.getProjectComponentManager().findByComponentName(project.getId(), "Google Account")
issue.setComponents([component.getGenericValue()]) 
 
//set description
issue.setDescription("description");

Caused by: javax.script.ScriptException: java.lang.NullPointerException
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:318)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:111)
	... 230 more

Any ideas?

Suggest an answer

Log in or Sign up to answer