Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do you set custom field as hidden and required if 5 out of 11 radio button options were picked

Diana
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.
April 14, 2022

I've searched all Atlassian Community groovy script suggestions and still could not find what works.

I'm using ScriptRunner Behaviors version 6.40.0 on Jira Server 8.13.8.

I have custom field A that is a single pick radio button with 11 options. If 5 out of 11 were selected, another custom field B should pop up as Required. The other 6 out of 11 options should have custom field B as hidden and not required.

But the script I used doesn't behave as desired.

import com.atlassian.jira.component.ComponentAccessor

def customFieldAID = getFieldByID(getFieldChanged())
def customFieldA = getFieldByName("Custom Field Radio Button")
def customFieldB = getFieldByName("Custom Field Hidden")
def selected = customFieldAID.getValue() as String


def selectCustomField = customFieldManager.getCustomFieldObject(customFieldA.fieldId)
def selectConfig = selectCustomField.getRelevantConfig(issueContext)
def selectOptions = ComponentAccessor.optionsManager.getOptions(selectConfig)


def fiveOptions = selectOptions.findAll { it.value in ['Option 1' || 'Option 2' || 'Option 3' || 'Option 4' || 'Option 5']}

if (fiveOptions == true) {
   customFieldB.setHidden(false)
   customFieldB.setRequired(true)
} else {
   customFieldB.setHidden(true)
   customFieldB.setRequired(false)

 

For some reason, it will not show custom field B at all. I've tried other variations that will only show up for Option 5 and not the rest.

Any suggestions?

1 answer

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 15, 2022

Hello @Diana Gorv ,

I think you are overcomplicating it. Have a behaviours linked to the custom field A with this script : 

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def customFieldA = getFieldById(getFieldChanged())
def customFieldAValue = customFieldA.getValue()
def customFieldB = getFieldByName("custom field B")


if (customFieldAValue in ["Option 1","Option 2","Option 3","Option 4","Option 5"]){
customFieldB.setHidden(false)
customFieldB.setRequired(true)
}
else {
customFieldB.setHidden(true)
customFieldB.setRequired(false)
}
Diana
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.
April 18, 2022

Thank you! This works perfectly!

Like Antoine Berry likes this

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, ace, atlassian community event, donation, girls who code, women in tech, malala fund, plan international, kudos, community badge, badge, atlassian badge, International Women’s month, International Women’s Day, women's month, women's day

10 for Change at Atlassian Community Events

Show up and give back by attending an Atlassian Community Event: we’ll donate $10 for every event attendee in March!

Join an Atlassian Community Event!
AUG Leaders

Upcoming Jira Events