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

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

1 vote

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 

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published 2 hours ago in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

17 views 0 1
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you