Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Control Form Field Based on Selected Checkbox Value

Features
Behaviours
Created 1 year ago, Updated 8 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.14)
compatibility bullet
ScriptRunner For Jira (6.18.0)
Language |
groovy
import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

// Get the logger and set the log level
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

// The name of the field to populate base on the value selected in the checkbox field associated with the script
final otherFieldName = 'Reason'

// Get the fields
def otherField = getFieldByName(otherFieldName)
def checkBoxField = getFieldById(fieldChanged)
def checkBoxFieldValue = checkBoxField.value

// Collect the selected checkbox values
log.debug("The checkbox field value: '${checkBoxFieldValue}' of type '${checkBoxFieldValue.class.simpleName}'")
def chosenValuesList = []
if (checkBoxFieldValue in String) {
    chosenValuesList.add(checkBoxFieldValue)
} else if (checkBoxFieldValue in ArrayList) {
    chosenValuesList.addAll(checkBoxFieldValue)
}

// If the user has selected "None" and nothing else, don't force to populate the other field:
if (chosenValuesList == ['None']) {
    checkBoxField.clearError()
    otherField.setRequired(false)
    otherField.setHidden(true)

// If the user has selected "None" and another value, show an error:
} else if ('None' in chosenValuesList) {
    checkBoxField.setError('You cannot select another value if "None" is selected"')
    otherField.setRequired(false)
    otherField.setHidden(true)

// If the user didn't select any value, or has selected some values, but NOT the "None" value, force to populate the other field:
} else {
    checkBoxField.clearError()
    otherField.setHidden(false)
    otherField.setRequired(true)
}
Having an issue with this script?
Report it here