Snippets

Matthew Clark (Adaptavist) Behaviour Initialiser script - Set the options on a checkbox field based on the underlying issues request Type (Jira Software view)

Created by Matthew Clark
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

String checkBoxFieldName = "CheckBoxA"

OptionsManager optionsManager = ComponentAccessor.getOptionsManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
FormField checkBoxField = getFieldByName(checkBoxFieldName)
CustomField checkBoxFieldCF = customFieldManager.getCustomFieldObjects().find {it.name == checkBoxFieldName}

def config = ComponentAccessor.fieldConfigSchemeManager.getRelevantConfig(issueContext, checkBoxFieldCF)
def options = optionsManager.getOptions(config)

def optionSet1 = [
    "TestB1",
    "TestB3"
]

//manualy defined name to the actual value from the getCustomFieldValue result
Map<String,String> requestTypeValueStore = ["Technical_Support":"tsd/technicalsupport"]

CustomField cfRequestType = customFieldManager.getCustomFieldObject(10009L)

if(underlyingIssue) {
    def requestType = getUnderlyingIssue().getCustomFieldValue(cfRequestType).toString()
    log.debug("Request Type = $requestType")

    if (requestType == requestTypeValueStore.Technical_Support) {
        def optionsToSet = options.findAll { it.value in optionSet1 }.collectEntries {
            [(it.optionId): it.value]
        }
        checkBoxField.setFieldOptions(optionsToSet) //now changing options
    }
}else{
    log.debug("Underlying issue is null")
}

Comments (0)