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 initialiser 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
1 issueContext is in the binding, and allows us to access the project and issue type of the current issue, or issue to be created
2 it goes without saying these components must exist for this to work
3 versions.last() is the most recently created version

Setting Defaults for Selects etc

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

In each case this involves find the ID of the Option object(s) that we want to set.

import com.atlassian.jira.component.ComponentAccessor

// set a select list value -- also same for radio buttons
def faveFruitFld = getFieldByName("Favourite Fruit") (1)
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObject(faveFruitFld.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionToSelect = options.find { it.value == "Oranges" } (2)

// same example but setting a multiselect - also same for checkboxes fields
def subComponentFld = getFieldByName("Subcomponent")
customField = customFieldManager.getCustomFieldObject(subComponentFld.getFieldId())
config = customField.getRelevantConfig(getIssueContext())
options = optionsManager.getOptions(config)
def optionsToSelect = options.findAll { it.value in ["Oranges", "Lemons"] } (3)
1 get the custom field by its name
2 the value of the option we want to default
3 same as above, but this time we are setting multiple options

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([12345, 67890])

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

For example, give parent A and child A1 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])

Have questions? Visit the Atlassian Community to connect, share, and learn with other Atlassian users and experts, including Adaptavist staff.

Ask a question about ScriptRunner for JIRA, Bitbucket Server, or Confluence.

Want to learn more? Check out courses on Adaptavist Learn, an online platform to onboard and train new users for Atlassian solutions.