Example Behaviour
You can find many example Behaviours scripts in the Example scripts section of the ScriptRunner website.
Example Behaviour Scripts
You can find some helpful example Behaviour scripts below. There are also many more example scripts available that can be accessed on our ScriptRunner HQ website.
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. When the Product option is selected, both fields are hidden.
const 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;
} Limit work item types based on user role
This script will limit the user's ability to create work items to a specific subset of work types if they belong to a certain group.
// Note: You should run this script by selecting "On load" in the check box above.
const context = await getContext();
const spaceId = context.extension.project.id;
const spaceRoles = await makeRequest(`/rest/api/3/project/${ spaceId }/roledetails?currentMember=true`);
const spaceRoleNames = spaceRoles.body.map(role => role.name);
const workItemTypeField = getFieldById("issuetype");
const allowedWorkItemTypeIds = ['10133', '10134']; // IDs for Story and Task
if (spaceRoleNames.includes('Developers')) {
workItemTypeField.setOptionsVisibility(allowedWorkItemTypeIds, true);
if (!allowedWorkItemTypeIds.includes(workItemTypeField.getValue()?.id)) {
workItemTypeField.setValue("10133"); // Default to Story if the current value is not allowed
}
}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 work item to that user.
const assigneeName = 'Demo User';
const res = await makeRequest(`/rest/api/3/user/search?query=${assigneeName}`);
const assigneeAccountId = res.body[0].accountId;
getFieldById("assignee").setValue(assigneeAccountId);Make the field read-only based on user groups
This script shows how you can get the user groups that the current logged-in user is a member of, and how to make fields read-only if the user is a member of a specific group.
const ticketDepartment = getFieldById("customfield 10205")
const userRegion = getFieldById("customfield 10206")
const ticketCat = getFieldById("customfield 10207")
const priority = getFieldById("priority")
const group = "Example Group";
const user = await makeRequest("/rest/api/2/myself");
if (user) {
const { accountId } = user.body;
const userGroups = await makeRequest ("/rest/api/2/user/groups?accountId=" + accountId);
if (userGroups) {
const groupNames = userGroups.body.map(({ name }) => name);
if (groupNames.includes(group)) {
ticketDepartment.setVisible(false)
userRegion.setVisible(false)
ticketCat.setVisible(false)
priority.setReadOnly(true)
}
}
}Set the description text below a field on the create screen
With this script, you can add some custom help text below a field to explain further context to users about what the field is for, then use the setDescription() method provided by behaviours.
getFieldById("summary").setDescription("Please describe the work item in less than 25 words");Show or hide fields conditionally based on the selection in another field
With this script, when a selection is made on a select field. It will show or hide another field based on the selection made.
// Retrieve the 'Opportunity' drop-down field and other relevant fields
const opportunityField = getFieldById("customfield_10057"); // Replace with your actual custom field ID
const contractValueField = getFieldById("customfield_10061"); // Field for 'Contract value'
const lossReasonField = getFieldById("customfield_10060"); // Field for 'Loss reason'
// Get the current selected value from the 'Opportunity' field
const opportunityValue = opportunityField.getValue()?.value;
// Control visibility based on the selected value
if (opportunityValue === 'Won') {
// If 'Won' is selected, show the 'Contract value' field and hide 'Loss reason'
contractValueField.setVisible(true);
lossReasonField.setVisible(false);
} else if (opportunityValue === 'Lost') {
// If 'Lost' is selected, show the 'Loss reason' field and hide 'Contract value'
contractValueField.setVisible(false);
lossReasonField.setVisible(true);
} else {
// If neither 'Won' nor 'Lost' is selected, hide both fields
contractValueField.setVisible(false);
lossReasonField.setVisible(false);
}Change field name for Jira Example
This example shows you how to change the name displayed for a specific field within the work item 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:
Change the Visibility of the Priority Field for Jira Example
You can use the Behaviours feature to control who can see or edit the Priority field based on their user permissions. For example, if you want only Space Managers to change the field, you can configure the behaviour to hide the field from all other users.
To do so, follow the steps below:
Now that you have created the behaviour, you will see that the Priority field is only visible to the Space Managers when creating an issue in Jira.
Make Fields Editable Based on Role for Jira Example
You can use the Behaviours feature to control field editability based on user roles. For example, as a Space Manager, you may want fields to be editable for users with the Developer role but read-only for those with the Administrator role. To achieve this, you'll need to check which space roles the logged-in user belongs to, then configure the behaviour to make fields editable for Developers and read-only for Administrators.
To do so, follow the steps below:
Auto-assign High Priority Work Items for Jira Example
You can use the Behaviours feature to automatically set the Assignee field when the Priority field is set to high and clear it for any other value. For example, as a Space Manager, you might want all high-priority work items to be assigned to the technical lead.
To do so, follow the steps below:
Show Warnings or Restrict Options for JSM Example
Use this example script to create a Behaviour that shows a warning or restricts the options available based on a selection in a select field. You will create a Behaviour that shows a warning or restricts the options based on the selections made in the Transport and Level fields.
Follow the steps below to create this behaviour:
Prefill Fields with User Information and Dates for JSM Example
Use this example to understand how to automatically populate fields in a Behaviour when creating or editing a work item.
- a user picker field with the current user
- a date field with today's date
Follow the steps below to create this behaviour:
Dynamically Update Priority for Incidents Summary for JSM Example
Use this example to create a Behaviour that automatically assigns the highest priority to incidents whose summary contains urgent keywords, such as 'outage' or 'system down'.
- If a keyword is found, it sets Priority to highest and makes the field read-only.
- If no keyword is found, it keeps Priority editable and sets it to medium when no priority has been selected.
Follow the steps below to create this behaviour:
Example Videos of Behaviours
We have included some example videos of Behaviour functionality. To help you understand the ScriptRunner for Jira Cloud Behaviours feature, you can also watch our Behaviours: Dynamic Field Control & Customization video playlist.
How to pre-fill a template in a field
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 doc for more information and watch the video below to understand how this works:Behaviours API
