Example Behaviour
Change Field Name Example
This example shows you how to change the name displayed for a specific field within the issue creation screen. In this example, the field entitled Summary is changed to Ticket Title in response to a particular team's preferences.
Follow the steps below to create the behaviour:
- Open the Behaviours tab and click the Create Behaviour button.
You will see the Create Behaviour screen displayed: - Enter a name and description for the behaviour. It's good practice to make these as descriptive as possible.
- Ensure the Enable Behaviour toggle is activated.
- Scroll down to the Behaviour Mapping section and select the relevant Project to which this behaviour will be mapped. For this example, choose the Customer Success project.
- Select the Issue Type that will be associated with the behaviour. For this example, choose the Task issue type.
- Scroll down to the Behaviours Scripts section and click Add Script. The Add Field Script pop-up window is displayed where you can add the behaviour script:
Define when the script should run. This can be when the issue creation screen loads initially and/or in response to a field change. For the purpose of this example, check the On load option so that the summary field is renamed when the issue creation screen loads.
When Runs On load The script will run when the create screen initially loads.
You'll want to choose this option when you want the affected field to populate immediately upon opening the create screen.
For example, a field name or field description is changed, or a value is pre-populated into the field.
On change The script will run when the specified supported field change happens.
You should choose this option when you've added a condition to the logic and identified a trigger that will update the affected field.
- Choose Create View from the options to run the script on Create View or Issue View (or both). Refer to the Supported Fields details, as not all field types are supported for Issue view.
- Enter your code within the script box, as required. Note that you can open the API documentation directly from here.
Alternatively, you can select an example script from the Example Scripts option and modify the code as required, ensuring that you:- edit any variables, like custom field names, roles, or groups, in the example code so it's relevant to your instance.
- choose the right time to run your script on load and/or change so that it runs when needed.
- Click Save Script once you have confirmed the parameters as
getFieldById("summary").setName("Ticket Title");.
Now that you have created the behaviour to run when the screen loads, you will see the field entitled 'Summary' change to 'Ticket Title' when you create new tasks within the Customer Success project. - Refresh your screen and click the Create button to see the behaviour in action. You are returned to the Create issue screen, where you will see your changes, as shown below:
If you wish to revert back to the original field name, simply click Edit from the ellipsis menu next to your chosen behaviour, as shown below:
and then deselect the Enable Behaviour button.
Example Videos
How to pre-fill a template on a field
Along with the example Behaviour and scripts outlined below, we have many demo videos aimed at helping you understand how the Behaviours feature works in ScriptRunner for Jira Cloud. Here's one of our Behaviours demo videos outlining How to pre-fill a template on a field:
Behaviours on screen tabs
One of our recent enhancements for Behaviours on ScriptRunner for Jira Cloud includes the addition of Behaviours on screen tabs. You can check out our Behaviours API doc for more information and watch the video below to understand how this works:
Example Scripts
We have provided a few example behaviour scripts below. However, you can find many more example scripts in the Adaptavist Library.
Dynamically show or hide a field
In this example, when the Department field is selected, and the Finance option is chosen, the line manager field is hidden, and the ticket category field is shown. When the HR option is selected, the ticket category field is hidden, and the line manager field is shown. Both fields are hidden when the product field option is selected.
jsconst departmentField = getFieldById('customfield_10035'); const ticketCategoryField = getFieldById('customfield_10037'); const lineManagerField = getFieldById('customfield_10036'); const changedField = getChangeField(); switch (changedField.getName()) { case 'Department': switch (changedField.getValue().value) { case 'Finance': lineManagerField.setVisible(false); ticketCategoryField.setVisible(true); break; case 'HR': ticketCategoryField.setVisible(false); lineManagerField.setVisible(true); break; case 'Product': ticketCategoryField.setVisible(false); lineManagerField.setVisible(false); break; } break; }
You can also refer to the video below:
Set the assignee field when the priority is set to High
This example configured on the On Change event shows how you can set the assignee field when the priority field is set to high and clear it when it is set to any other value.
const changedField = getChangeField();
const accountId = "123456-8545622-54522";
if (changedField.getName() === 'Priority') {
const priorityName = changedField.getValue().name.toString();
if (priorityName === "High") {
getFieldById("assignee").setValue(accountId);
}else{
getFieldById("assignee").setValue(null);
}
}
Make a field required when a value is selected inside a select list field
This example shows how you can make the ticket priority select list type field required when it has the Must Have value selected and optional when it has any other value.
const changedField = getChangeField();
if(changedField.getType() == "com.atlassian.jira.plugin.system.customfieldtypes:select"
&& changedField.getName() == "Ticket Priority"
&& changedField.getValue().value == "Must have") {
getFieldById("customfield_10038").setRequired(true);
}else{
getFieldById("customfield_10038").setRequired(false);
}
This example also works with radio button fields if you change the field type key in the script as follows:
"com.atlassian.jira.plugin.system.customfieldtypes:select"
to
"com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons"
Make a request to a Jira API
This example shows how you can call a Jira API to search for a user and assign the issue to that user.
const assigneeName = 'Demo User';
const res = await makeRequest(`/rest/api/3/user/search?query=${assigneeName}`);
const assignneAccountId = res.body[0].accountId;
getFieldById("assignee").setValue(assignneAccountId);
Set default description when an issue is created
This example allows you to set default text in the description field when an issue is created, ensuring the field is only updated when it has no value in it.
const descriptionValue = getFieldById("description").getValue();
// If description field is wiki markup access the content property
if (typeof descriptionValue !== "string") {
const descriptionValueContent = descriptionValue.content.toString();
if (!descriptionValueContent) {
getFieldById("description").setValue({
"version": 1,
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "As a ",
"marks": [
{
"type": "strong"
}
]
},
{
"type": "text",
"text": "<type of user>"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "I want ",
"marks": [
{
"type": "strong"
}
]
},
{
"type": "text",
"text": "<to achieve some goal>"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "So that ",
"marks": [
{
"type": "strong"
}
]
},
{
"type": "text",
"text": "<some reason is fulfilled>"
}
]
}
]
})
}
}