Behaviours API
You can reference the functions and properties outlined below within your behaviour scripts.
Read/update fields
Access a field
All supported fields can be accessed by using getFieldById(fieldID).
For example:
jsconst theDescription = getFieldById("description") theDescription.setName("The description name"); const theValue = "the value is " + theDescription.getValue(); logger.info(theValue);
Accessing custom fields
Custom fields cannot be accessed by their field name and instead have to be accessed by their ID e.g customfield_01232
The ID of the custom field can be found by either utilising the Jira Cloud REST API to make a request to the field endpoint https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-fields/#api-rest-api-3-field-get such as https://YOUR_ATLASSIAN_INSTANCE/rest/api/3/field
When selecting a custom field as the affected field, the custom field ID will display at the top of the Create modal screen.
Access a changed field
If your script is triggered onChange you can use the getChangeField() function. This function will return an object with the same structure as getFieldById.
For example:
jsconst changedField = getChangeField() if(changedField.getName() == "summary") { logger.info("The summary has changed!"); }
Available read methods
It is important to note that these methods are only accessible on the object returned by getFieldById.
The usage of the getValue and setValue methods has changed for several fields as the displayName property has been deprecated. The examples outlined below show how these now work.
For the assignee, userPicker and multiUserPicker fields, only accountIds are returned as the displayName property is no longer available. Scripts that rely on displayName information will break; instead, you will now have to make a request to /rest/api/3/user?accountId= and/or ${accountId}/rest/api/3/bulk?accountId==${idsToFetch.join("&accountId=")}.
For single and multiselect, you must now use the value property instead of the deprecated name property.
getContext
Returns the context details provided by Atlassian when a behaviours script is run.
Return Type: Object
{
localId: string,
cloudId: string,
enviroment: string,
moduleKey: string,
siteUrl: string,
accountId: string,
licence: string,
timeZone: string,
extension: {
viewType: string,
issueType: {
id: string,
name: string
},
project: {
id: string,
key: string,
type: string
}
}
}Note: When a behaviour is run on the Issue View inside the extension property of the context object, then there is an issue object with the structure, as shown below.
issue: {
id: string,
key: string,
}
jsconst context = await getContext()
Access the user that loaded the screen by account ID.
jsconst context = await getContext() context.accountId
Access the project key of the current project.
jsconst context = await getContext() context.extension.project.key
Access the ID of the current issue type.
jsconst context = await getContext() context.extension.issueType.id
Access the current issue key when on an issue view
jsconst context = await getContext() context.extension.issue.key
getDescription
Returns the description of the field
Return type: String
jsgetFieldById("summary").getDescription()
getId
Returns the id of the field
Return type: string
jsgetFieldById("summary").getId()
getName
Returns the name of a field
Return type: String
jsgetFieldById("summary").getName()
getOptionsVisibility
Returns the options that are visible for a field.
This method can only be used after setOptionVisiblity has been called; otherwise, it may be returned as undefined.
Return Type: Object
{options: Array<string>, isVisble: boolean}Supported Fields:
- Priority
- Issue Type
- Select List Fields
- Multi-Select List Fields
- Checkbox Fields
jsgetFieldById("priority").getOptionsVisibility()
jsgetFieldById("issuetype").getOptionsVisibility()
Note: This method works for On Change events only and always returns undefined for On Load events.
getType
Returns the type of the field.
Return type: string
jsgetFieldById("summary").getType()
getValue
Returns the value of a field. The return type depends on the field you’re updating:
| Field name | Return type | Example getValue() |
|---|---|---|
| Affects Versions | { id: string, name: string } |
|
| Assignee Field | null | { accountId: string } |
|
| Cascading Select | null | { parent: { id: string; value: string }; child: { id: string; value: string } | null } |
|
| Components Field | { id: string, name: string } |
|
| Custom Checkbox Field | { id: string, value: string }[] |
|
| Custom Date Picker Field | string | null |
|
| Custom Date Time Picker Field | string | null |
|
| Custom Multi User Picker Field | { accountId: string }[] |
|
| Custom Multiple Select Field | { id: string, value: string }[] |
|
| Custom Number Field | string | null |
|
| Custom Paragraph Field | string | ADParagraphField |
More details about ADF: https://developer.atlassian.com/cloud/jira/platform/apis/document/structure/ |
| Custom Radio Buttons Field | string | null |
|
| Custom Select Field | null | { id: string, value: string } |
|
| Custom Text Field | string | |
| Custom URL Field | string | null |
|
| Custom User Picker Field | null | { accountId: string } |
|
| Description | string | ADF |
More details about ADF: |
| Due Date | string | null |
|
| Fix Versions Field | { id: string, name: string } |
|
| Issue Type Field | { id: string, name: string } |
|
| Labels | string[] | |
| Original Estimate | number | null |
|
| Parent Field | { id: "10001", key: "DEMO-1000" } | null |
|
| Priority Field | { id: string, name: string, iconUrl?: string } |
|
| Project Picker Field | { projectId: string } |
|
| Reporter Field | null | { accountId: string } |
|
| Status Field | { id: string, name: string } |
|
| Summary | string |
|
| Target End Date | string | null |
|
| Target Start Date | string | null |
|
isCreateView
Returns whether or not the current view is the Create View.
Return type: boolean
jsif(isCreateView()){ getFieldById("summary").setValue("This code works only on the Create View"); }
isIssueView
Returns whether or not the current view is the Issue View.
Return type: boolean
jsif (isIssueView()){ getFieldById("summary").setValue("This code works only on the Issue View") }
isTransitionView
Returns whether or not the current view is the Transition View.
Return type: boolean
jsif(isTransitionView()){ const descriptionField = getFieldById("description"); descriptionField.setValue({ version: 1, type: "doc", content: [ { type: "paragraph", content: [ { type: "text", text: "This code works only on the Transition View" } ] } ] }); }
isReadOnly
Returns whether the field has been set as read-only.
Return type: boolean
jsgetFieldById("summary").isReadOnly()
isRequired
Returns whether or not the field is required.
Return type: boolean
jsgetFieldById("summary").isRequired()
isVisible
Returns whether the field has been hidden.
Return type: boolean
jsgetFieldById("summary").isVisible()
Available write methods
It is important to note that these methods are only accessible on the object returned by getFieldById and getChange.
The usage of the getValue and setValue methods has changed for several fields as the displayName property has been deprecated. The examples provided in the table below show how these now work.
For the assignee, userPicker and multiUserPicker fields, only accountIds are returned as the displayName property is no longer available. Scripts that rely on displayName information will break; instead, you will now have to make a request to /rest/api/3/user?accountId= and/or ${accountId}/rest/api/3/bulk?accountId==${idsToFetch.join("&accountId=")}.
For single and multiselect, you must now use the value property instead of the deprecated name property.
setDescription(desc)
Updates the field description.
Parameter: desc
Parameter type: string
jsgetFieldById("summary").setDescription("A new description")
setName(name)
Updates the field name.
Parameter: name
Parameter type: string
jsgetFieldById("summary").setName("A new name")
setOptionVisibility(options,isVisible)
Updates the values that are selectable in a field.
Parameter: options
Parameter type: Array<string> - Strings of the option value IDs
Parameter: isVisible
Parameter type: boolean
Note: setting isVisible to true shows the specified option values in the field only, whereas false hides the specified option values in the field.
jsgetFieldById("priority").setOptionsVisibility(["1","2"], true)
jsgetFieldById("issuetype").setOptionsVisibility(["12345","67890"], true)
setReadOnly(readable)
Sets a field to read-only.
Parameter: readable
Parameter type: boolean
jsconst summary = getFieldById("summary").setReadOnly(true)
Hidden fields
When the text (single), select list (single and multiple), checkbox, radio and number fields are set to read only but have no value in Issue View, they will be hidden.
setRequired(required)
Updates a field as to whether or not it should be required.
Parameter: required
Parameter type: boolean
jsgetFieldById("summary").setRequired(true)
setValue(value)
Updates the field value.
Parameter: value
Parameter type: Depends on the field you’re updating:
| Field Name | Return / Parameter Type | Example setValue() |
|---|---|---|
| Affects Versions | (value: string[] ) // Strings of version ids |
|
| Assignee | (value: string | null) |
|
| Cascading Select | (value: { parentId: string; childId: string | null;} | null) |
|
| Components | (value: string[] ) // Strings of component Ids |
|
| Custom Checkbox Field | string[] (option IDs) |
|
| Custom Date Picker Field | string (yyyy-mm-dd) |
|
| Custom Multiple Select Field | (value: string[]) |
|
| Custom Multiple User Picker | string[] (accountIds) |
|
| Custom Number Field | number |
|
| Custom Paragraph Field | string | ADF |
https://developer.atlassian.com/cloud/jira/platform/apis/document/structure/ |
| Custom Radio Buttons Field | string (option ID) |
|
| Custom Single Select Field | (id: string | null) |
|
| Custom Text Field | string | |
| Custom URL Field | string |
|
| Custom User Picker Field | string (accountId) |
|
| Description | string | ADF | |
| Due Date | string (yyyy-mm-dd) |
|
| Fix Versions | (value: string[] ) // Strings of version Ids |
|
| Issue Type | ( value: string | null) // String is the Issue Type ID |
|
| Labels | string[] | |
| Original Estimate | (value: number | null) |
|
| Parent | (id: string | null ) // String of parent Id |
|
| Priority | (value: string | null) |
|
| Project Picker Field | (projectId: string | null) |
|
| Reporter | (value: string | null) |
|
| Status | (transitionId: string ) // String of transition ID |
|
| Summary | string |
|
| Target End Date | string (yyyy-mm-dd) |
|
| Target Start Date | string (yyyy-mm-dd) |
|
setVisible(visible)
Updates the visibility of the field.
Parameter: visible
Parameter type: boolean
jsgetFieldById("summary").setVisible(false)
Behaviours on screen tabs
Demo video
You can watch our helpful demo video highlighting how the Behaviours on Screen Tabs feature works.
It is important to note that all Behaviours on screen tabs methods are only accessible on the object returned by getScreenTabById.
jstype ScreenTab = { getId: () => string; isVisible: () => boolean; setVisible: (isVisible: boolean) => void; focus: () => void; }
getId
Returns the screen tab identifier.
Return type: string
jsgetId(): string
isVisible
Returns true if the tab is currently visible. Returns false otherwise.
Return type: boolean
jsisVisible(): boolean
setVisible
Changes tab visibility.
Return type: void
jssetVisible(value: boolean): ScreenTabAPI tab.setVisible(false);
Do not hide in-focus tabs
Always ensure that you are not hiding the tab currently in focus. Doing so means your UIM won't be applied and the onError callback will receive a SCREENTABS_VALIDATION_FAILED error.
focus
Switches the focus to a given screen tab. Automatically puts other visible tabs out of focus.
Return type: void
jsfocus(): ScreenTabAPI tab.focus();
The screen tab setter methods are grouped and applied after the completion of other Behaviours that run on load or on change. This means that reading the values using the getter methods will always return the screen tab’s initial state.
You can refer to Atlassian's UI Modifications documentation for more details about screen tabs.
Make REST requests
You can use makeRequest to hit the Jira Cloud REST API.
Parameters:
url: string of the rest endpoint
requestOptions: Optional request options of type RequestInit | typescript - v3.7.7
Return type: Promise<{status: number, body: JSON }>
For example:
jsconst res = await makeRequest("/rest/api/2/myself"); if(res.body.accountId == "the accountId") { logger.info("User is bob"); }
Some Jira REST APIs are not supported on Atlassian Forge and will, therefore, not work on the Behaviours feature. For example, requests with OAuth2 permission scopes generally work on Forge. Where this scope is absent, we expect that the API endpoint is not supported on Forge.
You can make a POST request that specifies request options and headers with the makeRequest method. You can also make other types of REST requests, including PUT or POST. The example below shows how to make a POST request to the Jira expression API to test if an issue has more than 25 characters in the description and if so, to set some text in the summary field.
jsconst body = `{ "expression": "issue.description.plainText.length >25", "context": { "issue": { "key": "DEMO-1" // Specify the Issue key to test agains }, "project": { "key": "DEMO" // Specify the project key here for the project of the issue being tested against } } }`; const res = await makeRequest("/rest/api/3/expression/eval?expand=meta.complexity", { method: "POST", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: body }); if(res.body.value === false){ getFieldById("summary").setValue("Description field has less than 25 characters"); }else{ getFieldById("summary").setValue("Description field has more than 25 characters"); }
Logs
A logger is available, which will allow admins to view logs on the log page. This can be accessed by using the logger object.
Using the logger allows you to read the logs from your scripts inside the ScriptRunner Logs page.
For example:
logger.info("hello world");
Available functions - each method takes a string parameter:
warn(msg)
debug(msg)
info(msg)
trace(msg)
