Copy select list (cascading) custom field value from parent to sub-task on creation

Daphnis Hessling June 13, 2018

Hello Community,

We need some help with a Script Post-Function (ScriptRunner)..

We want to copy the value of a cascading select list custom field from the parent issue, whenever a sub-task is created.

We already searched the internet for a script that could help us, but we were unable to find a working solution. Copying "usual" custom field values from a parent issue to a sub-task is not a problem but the cascading type seems to be a bit tricky.

We are running Jira Server v7.10.0 with ScriptRunner v5.4.7.

Any help would be greatly appreciated.

 

Regards,
Daphnis

3 answers

2 accepted

3 votes
Answer accepted
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
June 13, 2018

@Daphnis HesslingThis worked for me. just put this in the substak's workflow, at creation postfunction, in the last place.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def parent = issue.getParentObject()
def cfCascade = customFieldManager.getCustomFieldObject(10109L)
def cascadeParent = parent.getCustomFieldValue(cfCascade) as Map

issue.setCustomFieldValue(cfCascade, cascadeParent)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

 

Daphnis Hessling June 14, 2018

Hi @Alejandro Suárez - TecnoFor,

Thank you very much for your script, it works like a charm. It does show an error though (please have a look at the attached screenshot). Furthermore it throws an error in the log, every time it is executed. Can you think of a way to prevent that from happening?

2018-06-14 09:12:10,681 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-06-14 09:12:10,681 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
groovy.lang.MissingPropertyException: No such property: issueManager for class: Script140
 at Script140.run(Script140.groovy:14)

Thanks again.

Regards,
Daphnis

Daphnis Hessling June 14, 2018

Script error.png

Mark Markov
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.
June 14, 2018

Hello, add right after def customFieldManager

def issueManager = ComponentAccessor.getIssueManager()
Daphnis Hessling June 14, 2018

Hi Mark,

thank you for your answer. The error from the screenshot disappeared. We are experiencing another exception in the log though.

2018-06-14 09:27:02,120 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-06-14 09:27:02,121 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.IllegalArgumentException: Source GenericValue can not be null.
 at com.atlassian.jira.association.NodeAssociationStoreImpl.getSinksFromSource(NodeAssociationStoreImpl.java:34)
 at com.atlassian.jira.project.version.DefaultVersionManager.getVersionsByIssue(DefaultVersionManager.java:752)
 at com.atlassian.jira.project.version.DefaultVersionManager.getFixVersionsFor(DefaultVersionManager.java:598)
 at com.atlassian.jira.project.version.DefaultVersionManager.updateIssueFixVersions(DefaultVersionManager.java:568)
 at com.atlassian.jira.issue.fields.FixVersionsSystemField.updateIssueValue(FixVersionsSystemField.java:98)
 at com.atlassian.jira.issue.fields.AbstractVersionsSystemField.updateValue(AbstractVersionsSystemField.java:368)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:708)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:673)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:659)
 at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:214)
 at com.atlassian.jira.issue.IssueManager$updateIssue$3.call(Unknown Source)
 at Script169.run(Script169.groovy:15)

But copying the value from the parent to the sub-task is still working. Do you have any idea how we can improve this script?

Regards,
Daphnis

Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
June 14, 2018

Hi @Daphnis Hessling, try with this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def parent = issue.getParentObject()
def cfCascade = customFieldManager.getCustomFieldObject(10109L)
def cascadeParent = parent.getCustomFieldValue(cfCascade) as Map

if (parent && cascadeParent) {
issue.setCustomFieldValue(cfCascade, cascadeParent)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

 

0 votes
Answer accepted
Daphnis Hessling June 14, 2018

Hi everyone,

Thank you two for the quick help. The following script works for us. We put it in the last place of the post functions right after the issue created event:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def parent = issue.getParentObject()
def cfCascade = customFieldManager.getCustomFieldObject(11800L)
def cascadeParent = parent.getCustomFieldValue(cfCascade) as Map

if (parent && cascadeParent) {
issue.setCustomFieldValue(cfCascade, cascadeParent)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

Regards,
Daphnis

1 vote
Mark Markov
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.
June 13, 2018

Hello @Daphnis Hessling

Copying value from one cascading field to another cascading field, can be done by "usual" method. But it seems youre tryin to perform something another. Like copy from cascading into single select.

Can you provide more details on your case? 

There is usual case. But if you need direct access to elements of cascading select list, you can get it like in log.error section:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def issue_parent = ComponentAccessor.getIssueManager().getIssueByCurrentKey("DS-2")
def issue_target = ComponentAccessor.getIssueManager().getIssueByCurrentKey("DS-8")
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("cascading select")
def cfvalue = issue_parent.getCustomFieldValue(cf)
issue_target.setCustomFieldValue(cf, cfvalue)
ComponentAccessor.getIssueManager().updateIssue(user, issue_target, EventDispatchOption.ISSUE_UPDATED, false)

log.error("Cascading Select parent value: " + cfvalue.get(null).toString())
log.error("Cascading Select child value: " + cfvalue.get("1").toString())
Daphnis Hessling June 13, 2018

Hi @Mark Markov,

Thank you very much for your fast response. We are actually looking for a post function, which copies the value from the parent to a created sub-task. The cascading field in the parent is the same as in the sub-task. So we just want to automatically fill the cascading field of the sub-task with the value of that same custom field of the parent issue.

I am not that much into coding, but if I understand your script right, we are not be able to use it in our case since it refers to exact issues:

def issue_parent = ComponentAccessor.getIssueManager().getIssueByCurrentKey("DS-2")
def issue_target = ComponentAccessor.getIssueManager().getIssueByCurrentKey("DS-8")

Nonetheless, thank you very much for your effort, we very much appreciate that. Maybe we can use your script in the future some day for another use case.

Regards,
Daphnis

Suggest an answer

Log in or Sign up to answer