Setting a Default Description

This simple example can be used as a tutorial. Further worked examples can be found in the Recipes section - Behaviours Examples.

  1. To create a new Behaviour. Go to the Administration screen, and click the Behaviours link in the Behaviours section, or press gg or . and type Behaviours.

  2. Enter a Name and Description for the new behaviour.

    new behaviour
  3. Click on Fields.

    click fields
  4. Click the Create Initialiser link.

    click create initialiser
  5. Enter the following in the Script section (leave the first two fields blank):

    def desc = getFieldById("description")
    
    def defaultValue = """h2. How to reproduce
        * step 1
        * step 2
    
        h2. Expected Result
    
        The widget should appear
    
        h2. Actual Result
    
        The widget doesn't appear""".replaceAll(/    /, '')
    
    if (!underlyingIssue?.description) { (1)
        desc.setFormValue(defaultValue)
    }
    1 don’t overwrite the description if it already exists
  6. Click Update.

  7. Click the Add one now link to map this behaviour to a project.

    add mapping
  8. Select one or more projects, then click Add Mapping.

    create mapping

    This is a very simple configuration and should be used to check everything is working. For information about Jira Service Desk mappings, see Using Behaviours with Service Desk.

  9. Create a new issue in the project associated with this behaviour. You should see the default description.

    default desc result

Using a Server-side Validator to set the Fix Versions Required

When you mark an issue as Resolved with the resolution of Fixed, you want the developer to specify a Fix Version. However, it doesn’t make sense to require a fix version when the resolution is Won’t Fix.

Solve this using a server-side validator to mark the Fix Version field as required, only when the resolution is Fixed.

  1. Create a new behaviour.

  2. Click Add Field, and add the Resolution field. Our server-side script, when written, is called the first time this field appears, and any time the user changes it in the dialog.

    add field resolution
  3. Click Add Server-side Script.

  4. Enter the following script:

    import com.atlassian.jira.issue.resolution.Resolution
    
    def resolutionField = getFieldById("resolution")
    def fixVersionsField = getFieldById("fixVersions")
    
    def resolution = resolutionField.getValue() as Resolution
    
    if (resolution.name == "Fixed") {
        fixVersionsField.setRequired(true)
        fixVersionsField.setHidden(false)
    } else {
        fixVersionsField.setRequired(false)
        fixVersionsField.setHidden(true)
    }

    This example sets the Fix Versions field to required and shown if the resolution is Fixed, and to optional and hidden if the resolution is anything else.

To test the script, Resolve an issue. Experiment with changing the resolution value. The Fix versions field should change accordingly. A good experiment now would be to modify the script so that instead of hiding the field it’s made read-only. To do, change setHidden to setReadOnly.

If you have created a new behaviour do not forget to map it to a project, or project/issue type combination.
You might want to do something similar whereby if the Duplicate resolution is selected, the Linked Issues field is shown, if there not already a Duplicate link for that issue.

Live Editing

Using an inline script can be painful as you have to keep clicking buttons and saving. It’s more productive to point to a file so it can be updated automatically.

script file
If you have a groovy script as opposed to a groovy class, as in the previous examples, the method name should be run.

When configured like this you can modify your code without even leaving the Resolve Issue dialog, let alone doing a page refresh.

The path to the script can be relative to a script root.
Tip for IDE users

If using an IDE you can get code completion by adding the following lines at the beginning of your script:

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

Have questions? Visit the Atlassian Community to connect, share, and learn with other Atlassian users and experts, including Adaptavist staff.

Ask a question about ScriptRunner for JIRA, Bitbucket Server, or Confluence.

Want to learn more? Check out courses on Adaptavist Learn, an online platform to onboard and train new users for Atlassian solutions.