Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Update multiselect field using behaviors

Vishal
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.
January 6, 2021

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 ?

1 answer

1 accepted

0 votes
Answer accepted
Radek Dostál
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.
January 6, 2021

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

 

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.

Vishal
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.
January 6, 2021

Hello @Radek Dostál

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.

Radek Dostál
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.
January 6, 2021 edited

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

Like Vishal likes this
Vishal
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.
January 6, 2021

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events