Setting Field Defaults
You can use Behaviours to set default values for system and custom fields, and you can set different defaults depending on the current user's role level or group.
You may want to set default values when you want end-users to modify them, or when you need to provide a structured template for input. You might also consider using the Read-only option to lock fields instead of setting values in post-functions. This approach is more user-friendly than setting the field in a post-function as it doesn't overwrite user-provided data.
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
You can use the following script to set default values for components, affected versions, and the assignee when creating a new issue in Jira. This script is designed to be used as an Initialiser script and run only once when the form is first loaded when a new issue is created. It checks the current action and exits if it's not the Create Issue action, ensuring it doesn't run on subsequent actions.
The script interacts with various Jira objects and fields using Jira's API and ComponentAccessor. You may need to modify the script based on your specific workflow or use action IDs (for example, getAction()?.id == 1
) instead of names for more precise control.
- From ScriptRunner, navigate to Behaviours.
- Select Create Behaviour.
- Enter a name for the behaviour. In this example we enter
Setting System Defaults
. - Optional: Enter a description for the behaviour.
- Select Create Mapping.
- Select the project and issue type(s) to map this behaviour to.
- Select Add Mapping to confirm the mapping.
- Select Create to create the behaviour.
You're taken to the Edit Behaviour screen where you can configure the behaviour further. Scroll to the Initialiser field and select Create Script.
Copy the following code into the inline script editor:
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")
Select Save Changes.
Setting defaults for single and multiple-choice lists
You can use the following script example to set default options for single and multiple-choice lists. The same example that works for a single select list will work for a radio buttons field; in addition, you can use the multiple-choice code to set a multi-checkbox field.
For single fields, set a single value; for multiple fields, use a List
.
- From ScriptRunner, navigate to Behaviours.
- Select Create Behaviour.
- Enter a name for the behaviour. In this example we enter
Setting List Defaults
. - Optional: Enter a description for the behaviour.
- Select Create Mapping.
- Select the project and issue type(s) to map this behaviour to.
- Select Add Mapping to confirm the mapping.
- Select Create to create the behaviour.
You're taken to the Edit Behaviour screen where you can configure the behaviour further. Scroll to the Initialiser field and select Create Script.
Use the following example to set default values:
// 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'])
Select Save Changes.
Setting cascading select list values
You can use the following script examples to set cascading select lists. This is similar to the above example, but slightly more complex due to parent and child options.
- From ScriptRunner, navigate to Behaviours.
- Select Create Behaviour.
- Enter a name for the behaviour. In this example we enter
Setting Cascading List Values
. - Optional: Enter a description for the behaviour.
- Select Create Mapping.
- Select the project and issue type(s) to map this behaviour to.
- Select Add Mapping to confirm the mapping.
- Select Create to create the behaviour.
You're taken to the Edit Behaviour screen where you can configure the behaviour further. - Choose if you want to set cascading select list values in an initialiser or server side script:
If you want to set values in an initialiser script:
Scroll to the Initialiser field and select Create Script.
Use the following example to set values:
getFieldByName("CascadingSelect").setFormValue(['AAA', 'A2'])
- If you want to set values in a server-side script:
- Scroll to Add Field.
- Find your cascading select list and select Add.
- Select Create Script.
Use the following example to set values:
groovyfield.setFormValue(["Parent option value", "Child option value"])
- Select Save Changes.
Set a read-only field based on another field
You can use the following example to set a read-only field (UserPicker) with the component lead's user name.
- From ScriptRunner, navigate to Behaviours.
- Select Create Behaviour.
- Enter a name for the behaviour. In this example we enter
Set a UserPicker field based on Component field
. - Optional: Enter a description for the behaviour.
- Select Create Mapping.
- Select the project and issue type(s) to map this behaviour to.
- Select Add Mapping to confirm the mapping.
- Select Create to create the behaviour.
You're taken to the Edit Behaviour screen where you can configure the behaviour further. - Scroll to Add Field.
- Find your Component/s field and select Add.
- Select Create Script.
Use the following script as a server-side script on the Component field to get the component lead:
import com.atlassian.jira.bc.project.component.ProjectComponent def formComponent = getFieldById(fieldChanged) def formUserField = getFieldByName("UserPicker") def components = formComponent.getValue() as List<ProjectComponent> // if any components have been set, set the user field to the component lead of the first component // otherwise unset it if (components) { formUserField.setFormValue(components.first().lead) } else { formUserField.setFormValue("") }
- Scroll Add Field again.
- Find the UserPicker field and select Add.
- Make the UserPicker field Readonly.
- Select Save Changes.
On selecting a component the field value should change:
If you remove all components it will be cleared.