Setting Field Defaults

Jira doesn’t make it easy to set defaults for system fields out of the box. Using Behaviours you can set default values for system and custom fields, and, set different defaults depending on the current user’s role level or groups.

Set default values when you want to allow the end user to change them from the initial values, or you want to provide a sensible structure for a multiline text field. Examples of this could be the correct phrasing for a user story or a bug report.

You can also "lock" the field by making it read-only. You could achieve the same result by setting the field in a post-function, however, this would overwrite any value the user had provided and is not very friendly.

Note that there is a longer worked example covering setting a default description in the behaviours overview page. This page is primarily to provide code examples for setting the different types of fields.

Setting System Field Defaults

When setting default values we use an initializer method. We want this code to fire once only, when the form is first loaded. Further, we check the name of the current action and exit the script if the action is anything but Create Issue - i.e. the user has clicked the Create button. You may need to alter this depending on your workflow, alternatively you could getAction()?.id == 1, to check the action by ID.

The following example covers how to set components, affects versions, and the assignee.

import com.atlassian.jira.component.ComponentAccessor

import static com.atlassian.jira.issue.IssueFieldConstants.AFFECTED_VERSIONS
import static com.atlassian.jira.issue.IssueFieldConstants.ASSIGNEE
import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS

if (getActionName() != "Create Issue") {
    return // not the initial action, so don't set default values
}

// set Components
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
def components = projectComponentManager.findAllForProject(issueContext.projectObject.id) // <1>
getFieldById(COMPONENTS).setFormValue(components.findAll { it.name in ["Support Question", "Frontend"] }*.id) // <2>

// set "Affects Versions" to the latest version
def versionManager = ComponentAccessor.getVersionManager()
def versions = versionManager.getVersions(issueContext.projectObject)
if (versions) {
    getFieldById(AFFECTED_VERSIONS).setFormValue([versions.last().id]) // <3>
}

// set Assignee
getFieldById(ASSIGNEE).setFormValue("admin")

Setting Defaults for Selects etc

The code required to set default options depends whether it’s a single or multi-select. The same example that works for a single select will work for a radio buttons field, likewise you can use the multi-select code to set a multi checkbox field. For "single" fields set a single value, for "multiple" fields use a List.

// set a select list value -- also same for radio buttons
def faveFruitField = getFieldByName('Favourite Fruit')
faveFruitField.setFormValue('Oranges')

// same example but setting a multiselect - also same for checkboxes fields
def subComponentField = getFieldByName("Subcomponent")
subComponentField.setFormValue(['Oranges', 'Lemons'])

Setting Cascading Select values

Cascading selects are similar to the above example, but a bit more involved as they have parent and child options.

The way to set a cascading select value is:

field.setFormValue(["Parent option value", "Child option value"])

where those two numbers are the IDs of the cascading select options.

For example, given parent AAA and child A2 options, this sets them:

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def fieldName = "testCascadingSelectList"
def field = getFieldByName(fieldName)
def customField = customFieldManager.getCustomFieldObjectByName(fieldName)
def fieldConfig = customField.getRelevantConfig(getIssueContext())

def options = optionsManager.getOptions(fieldConfig)
def parentOption = options.find { it.value == "A" }
def childOption = parentOption?.childOptions?.find { it.value == "A1" }

field.setFormValue([parentOption.optionId, childOption.optionId])

On this page