How to set value for radio button or check boxes in Behaviours plugin

William Rojas (Black Diamond) June 2, 2016

Hi, 

I am trying to access a radio button and/or a check box to then set value for other custom fields on our form using the Behaviours plugin, but can seem to get the right syntax ... this is the one scenario with a radio button

  • Solution is a single select field
  • Security Required is the radio button, values are: Yes, No, Do Not Know, defaults to None.
  • the snipet of Behaviours code:

FormField Solution = getFieldByName("Solution")
FormField Security = getFieldByName("Security Required?")
String SolutionVal = (String) Solution.getValue()
if (SolutionVal == "Solution 1" || SolutionVal == "Solution 2")

{ Security.setFormValue("Yes") }

else

{ Security.setFormValue("Do Not Know") }

1) what is the proper setFormValue syntax in this case
2) what would be the syntax of the equivalent getFormValue (say the radio was set to "Yes") -looking to be able to compare like (if radioVal == "Yes") { do something }
3) what is the syntax if instead of a radio button Security were a checkbox, so set the value for both "No" and "Do Not Know" -ignoring fact these labels are mutually exclusive
4) finally what is the equivalent getFormValue for retrieves all the values of a checkbox.
Thanks,

1 answer

4 votes
adammarkham
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.
June 2, 2016

Hi William,

You should attach the following script to your "APS Solution" field with behaviours:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.onresolve.jira.groovy.user.FormField

def solutionField = getFieldById(getFieldChanged()) // this is APS Solution 
def securityField = getFieldByName("MSM Security Required?") // radio button

def solutionValue = solutionField?.value
if (solutionValue == "Retail" || solutionValue == "Loyalty") {
    securityField.setFormValue(optionFor(securityField, "Yes").optionId)
} else {
    securityField.setFormValue(optionFor(securityField, "Do Not Know").optionId)
}

private Option optionFor(FormField securityField, String value) {
    def optionsManager = ComponentAccessor.getOptionsManager()
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def customField = customFieldManager.getCustomFieldObject(securityField.getFieldId())
    def config = customField.getRelevantConfig(getIssueContext())
    def options = optionsManager.getOptions(config)
    def optionToSelect = options.find { it.value == value }
    optionToSelect
}

There is also some examples in the documentation that can help you piece this together. First setting the values: https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/setting-default-fields.html#_setting_defaults_for_selects_etc 

Also setting value based on another field changing: https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/select-list-other.html

Hope this helps,
Adam 

Tal_Cohen April 15, 2019

Works great. Thanks! 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events