Copy all Component/s to Sub-Task, on creation

Judah
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 19, 2019

I want to copy each value in the "Component/s" field to a new sub-task on creation. I am able access & log the parent Component/s values, but cannot find the method used to SET the components in the sub-task? 

Example: Parent Story

story.PNG

Example: New Sub-task (not working)

subtask.PNGHere is my code. I need a method to SET the component/s for the subtask, please!

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.event.type.EventDispatchOption
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def issue = event.issue

def issueManager = ComponentAccessor.getIssueManager();
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def parentKey = issue.getParentObject()?.getKey()
def parentIssue = issueManager.getIssueObject(parentKey) // Parent issue object
def parentComponents = parentIssue.getComponentObjects()
def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)



if(issue.isSubTask()) {

log.debug(parentComponents)

//error is thrown on this method?? I need to SET the value
issue.setComponentObjects(parentComponents)
ComponentAccessor.getIssueManager().updateIssue(currentUser,myIssue,EventDispatchOption.ISSUE_UPDATED,true)

boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
indexingService.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)

}


else {
log.debug("ELSE")
}

 

And here is the log w/ err: 

2019-08-19 12:25:09,585 DEBUG [acme.CreateSubtask]: [ProjectComponentImpl { name='CAMS Project', description='', lead='', assigneeType='1', projectId='10305', id='14001' }, ProjectComponentImpl { name='FBCENTER Project', description='', lead='', assigneeType='1', projectId='10305', id='14003' }]
2019-08-19 12:25:09,588 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2019-08-19 12:25:09,588 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.atlassian.jira.bc.project.component.ProjectComponent
	at com.atlassian.jira.bc.project.component.DefaultProjectComponentManager$1.get(DefaultProjectComponentManager.java:73)
	at com.atlassian.jira.util.collect.TransformingIterator.next(TransformingIterator.java:34)
	at com.atlassian.jira.util.collect.CollectionUtil.foreach(CollectionUtil.java:39)
	at com.atlassian.jira.util.collect.CollectionUtil.toList(CollectionUtil.java:65)
	at com.atlassian.jira.util.collect.CollectionUtil.transform(CollectionUtil.java:127)
	at com.atlassian.jira.util.collect.CollectionUtil.transform(CollectionUtil.java:148)
	at com.atlassian.jira.bc.project.component.DefaultProjectComponentManager.updateIssueValue(DefaultProjectComponentManager.java:229)
	at com.atlassian.jira.bc.project.component.DefaultProjectComponentManager.updateIssueProjectComponents(DefaultProjectComponentManager.java:217)
	at com.atlassian.jira.issue.fields.ComponentsSystemField.updateIssueValue(ComponentsSystemField.java:438)
	at com.atlassian.jira.issue.fields.ComponentsSystemField.updateValue(ComponentsSystemField.java:411)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:704)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:669)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:655)
	at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:214)
	at com.atlassian.jira.issue.IssueManager$updateIssue$1.call(Unknown Source)
	at Script561.run(Script561.groovy:27)

 

3 answers

1 accepted

0 votes
Answer accepted
Judah
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 19, 2019

This code is working (despite some type-checking errors): 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.event.type.EventDispatchOption
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def issue = event.issue

def issueManager = ComponentAccessor.getIssueManager();
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def parentKey = issue.getParentObject()?.getKey()
def parentIssue = issueManager.getIssueObject(parentKey) // Parent issue object
def parentComponents = parentIssue.getComponents()
def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)




if(issue.isSubTask()) {

//log.debug(parentComponents)

issue.setComponent(parentComponents)
ComponentAccessor.getIssueManager().updateIssue(currentUser,issue,EventDispatchOption.ISSUE_UPDATED,true)

boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
indexingService.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)

}
0 votes
jira guy
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 19, 2019

Check your workflow post conditions. You might already have a plugin that can copy field values. 

 

I have Jira suite utilities plugin which allows me to add the below post function which I would add to sub-task workflow. 

Screen Shot 2019-08-19 at 3.07.46 PM.pngYou can also use Automation for JIRA plugin to achieve this. Not sure if the free version does this. 

Judah
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 19, 2019

Unfortunately I am using ScriptRunner for Jira. 

jira guy
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 19, 2019

Got it. I mentioned these plugins just because it is lot easier to achieve to same results. It's totally up to you on what you want to use. 

Judah
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 19, 2019

Thanks! 

0 votes
Sreenivasaraju P
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 19, 2019
Judah
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 19, 2019

That example works well if I know the component name and there is only ONE component. I need to take all of the parent components and add them as an array to sub-task...

Judah
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 19, 2019

If there is only one value in the "Component/s" field, it will update. But I cannot add the entire array of values when there exists, more than one. 

Suggest an answer

Log in or Sign up to answer