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
Example: New Sub-task (not working)
Here 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)
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)
}
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.
You can also use Automation for JIRA plugin to achieve this. Not sure if the free version does this.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Unfortunately I am using ScriptRunner for Jira.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Judah ,
Please refer below link, this may help
https://community.atlassian.com/t5/Jira-questions/Set-Component-in-groovy-script/qaq-p/689048
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.