Tutorial on safe access to a field

When you access the field of an issue, you might need to verify that it is not null before calling a method on the value of the field. This tutorial will guide you through writing Groovy scripts that access the fields of an issue avoiding exceptions due to a null value. 

On this page:

Imagine you want to access the username of the user, the issue is assigned to. Let us write a script for the same in the Groovy script tester of the JMWE Administration page.

Step 1 - Navigate to the Groovy script tester in the JMWE administration pages

  1. Go to the Administration icon  and click on it.
  2. Locate Add-ons from the menu and click on it.
  3. Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
  4. Click on Groovy script tester.

Step 2 - Write the script in the editor

  1. Write the following script in the editor.

    issue.get("assignee").name

Step 3 - Test your script

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-1
  3. Click on Test
  4. The following result will be displayed.

Avoid exceptions using the if structure

You can explicitly check for null using the if control structure and then call a method on the field value.

Step 1 - Fix your script using if structure

  1. Click on the editor.
  2. Replace the script with the following script

    if(issue.get("assignee")){
      issue.get("assignee").name
    }

Step 2 - Retest your script

  1. Click on Test again.
  2. The following result will be displayed. 

Avoid null pointer exception using the Safe navigation operator

You can simplify your script using the Safe navigation operator ? and avoid null pointer exceptions. Let us use it in the script and test it.

Step 1 - Simplify your script using the Safe navigation operator

  1. Click on the editor.
  2. Replace the script with the following script

    issue.get("assignee")?.name

Step 2 - Retest your script

  1. Click on Test again.
  2. The following result will be displayed. 


Avoid java.util.NoSuchElementException using the if structure

There might be cases where your script returns an empty collection and you will receive a java.util.NoSuchElementException if you call a method on the empty collection. You can avoid this using the if control structure only. For example, if you want to access the last comment on an issue and there are no comments for the issue you will face a java.util.NoSuchElementException

Step 1 - Write the script in the editor

  1. Write the following script in the editor.

    issue.get("comment").last()

Step 2 - Test your script

  1. Click on Test again.
  2. The following result will be displayed.


Step 3 - Try the Safe navigation operator

  1. Click on the editor.
  2. Replace the script with the following script

    issue.get("comment")?.last()

Step 4 - Retest your script

  1. Click on Test again.
  2. The same result will be displayed because using the Safe navigation operator does not fix the issue.

Step 5 - Fix your script using the if structure

  1. Click on the editor.
  2. Replace the script with the following script

    if(issue.get("comment")){
      issue.get("comment").last()
    }

Step 6 - Retest your script

  1. Click on Test again.
  2. The following result will be displayed

Safe navigation operator in a chained expression

Let us now write a script using the Safe navigation operator in an example that has a complicated chained expression. Imagine you want to access the Release date of the first Affects Version/s.

Step 1 - Write the script in the editor

  1. Write the following script in the editor.

    issue.get("versions").first().releaseDate

Step 2 - Test your script

  1. Click on Test again.
  2. The following result will be displayed.


Step 3 - Apply the Safe navigation operator to the first() method

  1. Click on the editor.
  2. Replace the script with the following script

    issue.get("versions")?.first().releaseDate

Step 4 - Retest your script

  1. Click on Test again.
  2. The following result will be displayed because you cannot get releaseDate on a null object

Step 5 - Apply the safe navigation operator to the releaseDate

  1. Click on the editor.
  2. Replace the script with the following script

    issue.get("versions")?.first()?.releaseDate

Step 6 - Retest your script

  1. Click on Test again.
  2. The following result will be displayed

Next >> Looping over collections