Trying to save select value in a Scriptrunner behaviour
Asked by Peter Ellis:

Hi,

What should be very simple has proved challenging getting the right syntax. I am successfully picking up the select fields value from the parent issue but despite the many variations I have tried to use I cannot get it copied over to the child issue,. Can someone tell me what I have done wrong?

def fldCustomer = contextIssue.getCustomFieldValue('Customer')  
def fldCustomerValue = fldCustomer.toString()  
getFieldByName('Customer').setFormValue(fldCustomerValue)  
I have also tried using fldOptions below:

def fldOptions = optionsManager.findByOptionValue(fldCustomerValue)\[\]


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Peter Ellis](/t5/user/viewprofilepage/user-id/4659760)

What you are trying to achieve is not directly doable via ScriptRunner Behaviour for the Issue Create screen. It is only doable on an Issue Update screen because the Sub-task is not yet linked to the parent issue on the create screen and cannot invoke the field values from the parent issue.

On the other hand, if you have a scripted field, i.e. like the issue picker, you can use it to indicate the parent issue and pass the value from it. I have provided a solution for this in this [community post](https://community.atlassian.com/t5/Jira-questions/How-to-copy-field-from-parent-to-subtask-on-create-screen-using/qaq-p/2504945).

The other alternative would be to use the Post-Function, e.g. on the Create transition, where once the issue is created, it will pass the field values from the parent issue to the child issue.

Below is a very simple code sample using the Custom Script Post-Function for the Create transition:-  

```
if (issue.subTask) {
    def parent = issue.parentObject
    issue.set {
        setCustomFieldValue('Sample Text Field', parent.getCustomFieldValue('Sample Text Field'))
    }
}
```

I am looking forward to your feedback.

Thank you and Kind regards,  
Ram

comments: author: Peter Ellis
body: Hi Ram,

Thank you for replying. I am currently copying over text and date data from the parent to the linked issue without any problem using script, see images. I just assumed that there is another syntax for select fields. I could use a post function but if there is a way to have this all in the same place that would be preferred.

![SR_1.png](https://community.atlassian.com/t5/image/serverpage/image-id/344743iC1D5CAFD9A18D356/image-size/large?v=v2&px=999 "SR_1.png")![SR_2.png](https://community.atlassian.com/t5/image/serverpage/image-id/344744iC3D13EC39B27AF92/image-size/large?v=v2&px=999 "SR_2.png")![SR_3.png](https://community.atlassian.com/t5/image/serverpage/image-id/344742iDD44F60B5828CF86/image-size/large?v=v2&px=999 "SR_3.png")


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Peter Ellis](/t5/user/viewprofilepage/user-id/4659760)

From the screenshot you have shared, you seem to be trying to pass the value during the create screen.

As I mentioned in my previous comment, this will fail when using Behaviour because when the Create screen is displayed, the link between the parent and child issue is not yet established as the child issue doesn't yet exist.

Hence, Behaviour is not a suitable approach for this. You must instead use the Post-Function or the Listener.

However, if you intend to use Behaviour, it can only work when using the Edit screen or Transition screen.

I am looking forward to your feedback.

Thank you and Kind regards,  
Ram


author: Peter Ellis
body: Hi Ram,

I didn't mention earlier but I am creating new issues via fragments so the parent is still in context when the related issue is created but not submitted.

I knew what I needed to do in the code for a select field (create an array, find the option ID and return it etc) I eventually worked it out and it's all working perfectly now.

![SR_1.png](https://community.atlassian.com/t5/image/serverpage/image-id/348952iC526367C9BA00E0C/image-size/large?v=v2&px=999 "SR_1.png")![SR_2.png](https://community.atlassian.com/t5/image/serverpage/image-id/348951iF5E5E55A867B74F5/image-size/large?v=v2&px=999 "SR_2.png")![SR_3.png](https://community.atlassian.com/t5/image/serverpage/image-id/348953iD4FAC9B8C434616C/image-size/large?v=v2&px=999 "SR_3.png")![SR_4.png](https://community.atlassian.com/t5/image/serverpage/image-id/348950iAD7249AEC2AF8584/image-size/large?v=v2&px=999 "SR_4.png")


Tags: behaviours

data-center

jira-data-center

script

scriptrunner