Dynamic Select List Behaviour

I’m creating a new behaviour that does multiple dynamic things.

  1. Load a map(key/value pairs) from a shared script and sets that as options for a single select field.
  2. Based on what is chosen, a text field should be updated.

So far, I’ve managed to populate the last based on the map as entered in the script file.

I’ve also managed to trigger an event which enters the switch in my code to decide what to write in the text field. However, the value I’m getting there is always null. Weird thing is, the event is only triggered when following these steps:

  1. Open the view screen
  2. Select an option other then the default.
  3. Select the default option again.
  4. Event gets triggered, switch statement is checked and value is null.

The initiator script as follows:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

import Constants.TemplateDescriptions

@BaseScript FieldBehaviours fieldBehaviours

final listFieldName = 'List'
final descriptionFieldName = 'Description'

def cfManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

def listField = getFieldByName(listFieldName)
def descriptionField = getFieldByName(descriptionFieldName)

// Make sure the fields we want to work with are on the form
if (!listField && !descriptionField) {
    return
}

listField.setFieldOptions(ListClass.keyValueMap)

And for the Single Select List I have this script:

import java.text.SimpleDateFormat

final listFieldName = 'Description Template List'
final descriptionFieldName = 'Description'

def listField = getFieldByName(listFieldName)
def descriptionField = getFieldByName(descriptionFieldName)


// Make sure the fields we want to work with are on the form
if (!listField && !descriptionField) {
    return
}

def date = new Date()
def sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")

def input = listField.getValue();
switch (input) {
    case 'Option 1':
        descriptionField.setFormValue('Option 1')
        break;
    case '-1':
        descriptionField.setFormValue('-1')
        break;
    case 'null':
        descriptionField.setFormValue('null');
        break;
    case -1:
        descriptionField.setFormValue('-1 int')
        break;
    case null:
        descriptionField.setFormValue('null null ' + sdf.format(date) + ' input: ' + input);
        break;
    default:
        descriptionField.setFormValue('input:' + input + '\nreverting to default because value is ' + listField.getFormValue() + ' changed value ' + changedField.getValue() + ' value ' + listField.value);
}

I’m wondering whether my desired behaviour is actually possible.

Hi.
Can you share how you have the field configured in the behavior?

Well, in the end I figured out that the way I’ve added the options, didn’t really work and now switched to these implementations which seem to work.

Initialiser.groovy

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

import constants.TemplateDescriptions

@BaseScript FieldBehaviours fieldBehaviours

// Fieldnames as used within the Jira configuration
final descriptionTemplateListName = 'Description Template List'
final descriptionFieldName = 'Description'

def cfManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def issueService = ComponentAccessor.getIssueService()

def descriptionTemplateListField = getFieldByName(descriptionTemplateListName)
def descriptionField = getFieldByName(descriptionFieldName)
def cfDescriptionTemplateListField = cfManager.getCustomFieldObjectsByName(descriptionTemplateListName).getAt(0)

// Make sure the fields we want to work with are on the form
if (!descriptionTemplateListField && !descriptionField && !cfDescriptionTemplateListField) {
    return
}

// Remove all current options from the current dropdown to prevent duplicates
def fieldConfig = cfDescriptionTemplateListField.getRelevantConfig(getIssueContext())
optionsManager.removeCustomFieldOptions(cfDescriptionTemplateListField)

// Build up option list from constants
def optionList = [] as Set
TemplateDescriptions.optionTemplateMap.eachWithIndex{entry, index ->
    optionList.add(optionsManager.createOption(fieldConfig, 0, (long) index, entry.key as String))
}

descriptionTemplateListField.setFieldOptions(optionList)

And the code for the dropdown list field:

import constants.TemplateDescriptions

// Fieldnames as used within the Jira configuration
final descriptionTemplateListName = 'Description Template List'
final descriptionFieldName = 'Description'

def descriptionTemplateListField = getFieldByName(descriptionTemplateListName)
def descriptionField = getFieldByName(descriptionFieldName)

// Make sure the fields we want to work with are on the form
if (!descriptionTemplateListField && !descriptionField) {
    return
}

def selectedOption = descriptionTemplateListField.getValue();
def originalDescriptionContent = descriptionField.getValue();
def selectedTemplate = TemplateDescriptions.optionTemplateMap[selectedOption] ?: ""
descriptionField.setFormValue(
    (originalDescriptionContent ? 'START>>\n\n' + originalDescriptionContent + '\n\n<<END\n\n' : '') +
    selectedTemplate
)

It would be good to know if things can be improved though.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.