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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
Regards.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
Regards,
Sudharsan.G
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This script does not work on cloud, How to configure the same requirement in JIRA cloud.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.