Update multiselect field using behaviors
Asked by Vishal:

Hello Guys,

I have a multiselect field with values A, B, C, D, \& so on, I need to update this field with values based on some condition.

This field should be updated with specific values from the list based on some other conditions \& become read only.

I need to do this using behavior at the time of issue creation itself. Does anyone know how to do this ?


Answers:
author: Radek Dost?l
body: I would recommend reading through the documentation: <https://scriptrunner.adaptavist.com/6.16.0/jira/tutorials/behaviours-tutorial.html>

And by that I mean - it's quite important to understand how they work, otherwise you will find it quite hard to modify them later on.

A few hints though:

- you probably need to set up a Server-side script Initializer **for** your "condition" field

- meaning that if your script needs to work whenever field X changes, then you need to add a server side script to the field X, server side script will "listen" to when changes happen to that field

- if you have multiple fields in your condition, then you would want to create a groovy script for each of them, again the reason is - the code runs whenever the field's value changes, so multiple implementation is there to make it work regardless of which order they are filled out

- in the groovy script, you can make it work for only Issue Create form via wrapping your code in 'if (getAction()?.id == 1)', where the Create transition always has the ID of 1 (unless somebody broke the backend to change that, so no it's always 1)

- there are Behaviour examples in the documentation, which will get you going: <https://scriptrunner.adaptavist.com/6.16.0/jira/recipes/behaviours.html>, such as [Setting Cascading Field values](https://scriptrunner.adaptavist.com/6.16.0/jira/recipes/behaviours/setting-default-fields.html#_setting_cascading_select_values)

Let me know in case you stumble on something - I could give a hint or help fixing your groovy code, I cannot do this without understanding the conditions and what where should work how (functionally). In any case, it's always good if you try first and we can fix things on the go.

comments: author: Vishal
body: Hello [@Radek Dost?l](/t5/user/viewprofilepage/user-id/715713)

I have code ready \& is working fine, the only challenge that I am facing now is to update multiselect field. I would need some solution to that.

I have two fields : One radio button with options "Yes / No" , second multiselect list field with options lets say " A, B, C till Z ", if user selects "Yes" as a response to first field, the multiselect field should be updated automatically with values "A,B,C, F,G,Z" \& become read only.

Everything is working fine except for the multiselect field update, I need help on that.


author: Radek Dost?l
body: Following seems to work for me:

```
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def multiSelectCF = getFieldByName("<MULTISELECT FIELD NAME>")
def customField = customFieldManager.getCustomFieldObject(multiSelectCF.getFieldId())

def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionsToSelect = options.findAll { it.value in ["option 1", "option 2"] }
multiSelectCF.setFormValue(optionsToSelect*.optionId)
```

This was from <https://scriptrunner.adaptavist.com/6.16.0/jira/recipes/behaviours/setting-default-fields.html#_setting_defaults_for_selects_etc>


author: Vishal
body: This worked like a charm Radek, thanks a lot for your help. :)


Tags: confluence-server

jira-server

server