[All Adaptavist Apps]

Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

You can reference the following functions and properties outlined below within your behaviour scripts.

Read/Update fields

Access a field

All supported fields (description, summary, assignee, labels, priority) can be accessed by using getFieldById(fieldID).

For example:

Code Block
language

Methods

getName()

Returns the name of the affected field.

Return type

String

getDescription()

Returns the description of the affected field.

Return type

String or Atlassian Document Format object depending on how your instance is configured

getValue()

Returns the value of the affected field.

The type of value depends on the affected field you have chosen.

js
const theDescription = getFieldById("description")
theDescription.setName("The description name");
const theValue = "the value is " + theDescription.getValue();
console.log(theValue);

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:

Code Block
languagejs
const changedField = getChangeField()
if(changedField.getName() == "summary") {
	console.log("The summary has changed!");
}

Available Read Methods

Note that these are only accessible on the object returned by getFieldById.

MethodDescriptionExample
getValue

Returns the value of a field

Return type depends on the field you’re updating:

  • Summary: string

  • Description: string

Affected fieldTypeSummaryStringDescriptionString
  • or Atlassian Doc Format depending on

how your instance is configured.
  • your Jira configuration

  • Labels

String
  • : string[]

  • Priority

{

     iconUrl
  • : { name: string,

     name: string,

     id
  • id: string

  • }

  • Assignee

  • : {

   
  • accountId: string,

   
  • displayName: string

,

}

...

  • }

Code Block
languagejs
getFieldById("summary").getValue()

getName

Returns the

...

name of

...

a field

...

Return type

String

isVisible()

Returns a boolean indicating whether or not the affected field is visible.

Return type

boolean

getFieldById(fieldId)

Returns information about a specific field and then allows you to call the set methods outlined below on that field to set properties on the field.

 Parameters

...

Return type
Code Block
{

     getName(): string,

     getDescription(): string,

     getID(): string,

     getValue(): depends on the field, see table in getValue above

     isVisible(): boolean

}
getFieldById(fieldId).setName(name)

Updates the name of the affected field.

Parameters

...

: String

Code Block
languagejs
getFieldById("summary").getName()

getDescription

Returns the description of the field

Return type: String

Code Block
languagejs
getFieldById("summary").getDescription()

isVisible

Returns whether the field has been hidden

Return type: boolean

Code Block
languagejs
getFieldById("summary").isVisible()

getId

Returns the id of the field

Return type: string

Code Block
languagejs
getFieldById("summary").getId()

Available Write Methods

Note these are only accessible on the object returned by getFieldById and getChange.

Info
If you are setting a field value which is in Atlassian Doc Format, Atlassian provides a tool that allows you to generate the ADF. You can read more in Atlassian's documentation.
getFieldById(fieldId).setDescription(description)

Updates the description of the affected field

Parameters

...

MethodDescriptionExample

setName(name)

Updates the field name

Parameter: name

Parameter type: string

Code Block
languagejs
getFieldById("summary").setName("A new name")

setDescription(desc)

Updates the field description

Parameter: desc

Parameter type: string


Code Block
languagejs
getFieldById("summary").setDescription("A new description")



setVisible(visible)

Updates the fields visibility

Parameter: visibile

Parameter type: boolean

Code Block
languagejs
getFieldById("summary").setVisible(false)

setValue(value)

Updates the field value

...

The value parameter will depend on the affected field you have chosen.

Parameter: value

Parameter type: Depends on the field you’re updating

  • Summary: string

  • Description: string

Affected fieldTypeSummaryStringDescriptionString
  • or Atlassian Doc Format depending on

how your instance is configured.
  • your Jira configuration

  • Labels

String
  • : string[]

  • Priority

{

     iconUrl
  • : { name: string,

     name
  • id: string

,

{

   
  • }

     id: string

}

assignee
  • Assignee: { accountId: string,

   
  • displayName: string

,
  • }

Code Block
languagejs
}
getFieldById(

...

"summary").

...

setValue(<FieldValue>)

...

Hides the specified field if is visible on the current screen.

getFieldById(fieldId).setVisible()

shows the specified field if is visible on the current screen.

makeRequest(path)

Used to make a request to the Jira Cloud REST API.

Note
titleCaveats
  • Currently, the makeRequest() method only supports making GET requests.
  • Currently, the makeRequest() method only supports calling the Jira Core Cloud or Jira Software Cloud Rest API's
Parameters

...

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:

Code Block
languagejs
const 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.

Code Block
languagejs
const 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)

Return Type

Promise<{ status: number, body: JSONObject}>

Parameters

logger

An object containing functions used for logging

Available logger functions

...