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.*

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")
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

Restricting Issue Types

It’s a common requirement that you would like non-core members of the project to only create certain issue types, whereas the developers should be able to create Bugs, New Features etc.

In this example we check the roles in the current project of the remote user - if they are members only of the Users role, we set the issue type to Query and lock the field.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE

// if the current user is in the Users role only, set the issue type to "Query", and lock it
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def user = ComponentAccessor.jiraAuthenticationContext.user

def remoteUsersRoles = projectRoleManager.getProjectRoles(user, issueContext.projectObject)*.name
if (remoteUsersRoles == ["Users"]) {
    def constantsManager = ComponentAccessor.getConstantsManager()
    def queryIssueType = constantsManager.getAllIssueTypeObjects().find { it.name == "Query" }
    getFieldById(ISSUE_TYPE).with {
        setFormValue(queryIssueType.id)
        setReadOnly(true)
    }
}

For people only in the Users role the Create Issue dialog will look like this:

locked issue type
If this is critically important that people only create the allowed issue types, you should consider backing it up with a workflow validator (perhaps a simple scripted validator) . Remember Behaviours are only in effect when using the UI, not when using REST.

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)
faveFruitFld.setFormValue(optionToSelect.optionId)

// 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)
subComponentFld.setFormValue(optionsToSelect*.optionId)
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])

For how-to questions please ask on Atlassian Answers where there is a very active community. Adaptavist staff are also likely to respond there.

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