Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

1 answer

1 accepted

2 votes
Answer accepted
Elifcan Cakmak Community Leader Apr 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.

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.

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.

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 Community Leader Apr 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.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Software

Presenting the "Best of 2020" Jira Software roundup!

Catch up with Atlassian Product Managers in our 2020 Demo Den round-up! From Advanced Roadmaps to Code in Jira to Next-Gen Workflows, check out the videos below to help up-level your work in the new ...

1,140 views 2 7
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you