Copying parent and child parts of cascading select custom field to 2 separate custom fields

Sandra Meessen April 29, 2019

Hi all,

I have a cascading custom field and 2 separate single select custom fields. I want to copy the parent part of the cascading cf to 1 separate single select custom field and copy the child part of the cascading cf to the other single select custom field. 

Copying the child part works correct via the JSU post function "Copy value from other field"

Copying the parent part I didn't find a solution yet. Who can help me? I am not experienced in Scriptrunner coding, I could use your help there.

Thank you and kind regards, Sandra

2 answers

1 accepted

2 votes
Answer accepted
Elifcan Cakmak
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.
April 29, 2019

Hello,

Let's assume you have a cascading field with values:

a - 1, 2

b - 3, 4

c - 5,6

And two single select list customfields with values:

a, b, c,

1,2,3,4,5,6

In this scenario you can use below script to set single select lists based on the cascading field values:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("DSP-4")
def userManager = ComponentAccessor.getUserManager()
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def user = authenticationContext.getLoggedInUser()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()


def cascading = customFieldManager.getCustomFieldObject("customfield_11303")
def cascadingLevel1 = customFieldManager.getCustomFieldObject("customfield_11304")
def cascadingLevel2 = customFieldManager.getCustomFieldObject("customfield_11305")

def cascadingValue = cascading.getValue(issue)
def cascadingLevel1toSet = cascadingValue[null]
def cascadingLevel2toSet = cascadingValue["1"]

def option1 = optionsManager.findByOptionValue(cascadingLevel1toSet.toString())
def option2 = optionsManager.findByOptionValue(cascadingLevel2toSet.toString())


issue.setCustomFieldValue(cascadingLevel1, option1[0]);
issue.setCustomFieldValue(cascadingLevel2, option2[0]);

issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

You can test it on script console. You need to change the issue key to test and customfield ids.

Regards.

Sandra Meessen April 29, 2019

Hi @Elifcan Cakmak , Thank you for you quick and extensive reply. How do I know the custom field ID of the parent part and of the child part? I know how to see the custom field ID of the complete field (via "edit" and check the number in the URL). 

And do I put this piece of coding in a scriptrunner post function in the workflow?

Thank you and kind regards.

Elifcan Cakmak
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.
April 29, 2019

Hello @Sandra Meessen  

You don't need id for parent and child. What you use here is the id of customfields. What you are referring to as parent and child are the options(values) of the cascading custom field. See this part:

def cascading = customFieldManager.getCustomFieldObject("customfield_11303")
def cascadingLevel1 = customFieldManager.getCustomFieldObject("customfield_11304")
def cascadingLevel2 = customFieldManager.getCustomFieldObject("customfield_11305")

cascading: change it to your cascading custom field's id

cascadingLevel1: change it to your single select list custom field id that you want to copy the parent part of the cascading field

cascadingLevel2: change it to your single select list custom field id that you want to copy the child part of the cascading field

See example screenshot below:

test.PNG

You need to remember that your single select list custom field should contain the values that will come from the cascading field as I explained above.

After you test it on script console, you can put it on the post function as you stated. But you need to remove this part when you put it there:

def issue = issueManager.getIssueObject("DSP-4")

This part is to test the code with a specific issue. When you remove it, script will get the issue itself during transition.

Hope this makes sense.

Regards.

Sandra Meessen April 30, 2019

Hi @Elifcan Cakmak , thank you for your clear explanation, I am trying this in our Script Console, but I get this error message. What am I doing wrong? Thank you and regards, Sandra Screenprint Script console.png

Elifcan Cakmak
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.
April 30, 2019

Hello @Sandra Meessen  

I am getting the same error (static type checking) on script console, it does not prevent the script working. However the below error on the result is different. Can you try to return option 1 with

return option1

 Here is my screenshot. See below option1 returns as a list [b, b]. That's why we are using option1[0] below while setting the custom field value. 

Selection_290.pngRegards.

Kevin Carroll November 13, 2021

Hello @Elifcan Cakmak 

Thanks for helping with this script.  Could you please help me understand how to modify this script to perform this update on all issues within a single project at once?  I want to run this through the console.

 

Thanks!

Anjaneyulu.k November 19, 2021

Hello Everyone,

I have used above script to copy field values from cascading field but I need to update the "Issue Key" automatically, is there any possibilities for this action?

It would be helpful for us.

Thanks in Advanced.

Regards,

Anji 

Anjaneyulu.k November 19, 2021

@Elifcan Cakmak @Kevin Carroll @Sandra Meessen 

Can you please look into the above issue.

Sudharsan December 16, 2021

Hi @Elifcan Cakmak ,

Can we copy cascading first filed value to a text box in same issue after creating the issue ?

Cascading filed : X Y Z  X1 X2 X3 Y1 Y2 Y3 Z1 Z2 Z3

I need to copy the values X or Y or Z to text filed Using Post function script 

 

I have asked a question 

https://community.atlassian.com/t5/Jira-Software-questions/Can-we-copy-cascading-first-filed-value-to-a-text-box-in-same/qaq-p/1889576#M175588 

Regards,

Sudharsan.G

0 votes
Sujata birajdar January 21, 2022

This script does not work on cloud, How to configure the same requirement in JIRA cloud.

subham patra April 6, 2022

Jira Cloud has an easy solution for this. You can use out-of-box Automation feature. 

Please refer to this: https://confluence.atlassian.com/jirakb/automation-for-jira-copy-parent-value-of-cascading-field-to-another-custom-field-1035240617.html 

Suggest an answer

Log in or Sign up to answer