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.
This page primarily provides code examples for setting the different types of fields.
There is a worked example covering setting a default description on the behaviours tutorial page.
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 (underlyingIssue) {
return // the issue already exists, therefore this is not the initial action, so don't set default values
}
// set Components. If the named components are not valid for this project, they won't be set
getFieldById(COMPONENTS).setFormValue(["Support Question", "Frontend"])
// set "Affects Versions" to the latest version
def versionManager = ComponentAccessor.getVersionManager()
def versions = versionManager.getVersions(issueContext.projectObject)
if (versions) {
// versions.last() is the most recently created version
getFieldById(AFFECTED_VERSIONS).setFormValue([versions.last().id])
}
// 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 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. 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:
groovyfield.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:
getFieldByName("CascadingSelect").setFormValue(['AAA', 'A2'])