Tutorial on safe access to a field

Cloud Migration Resources

Planning a Jira Cloud migration? These resources can help you get started:

JMWE Cloud features – Review Cloud features and understand key differences between DC and Cloud.
Migration support from Appfire – Learn how we can help you migrate smoothly.

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

Need support? Create a request with our support team.

Copyright © 2005 - 2026 Appfire | All rights reserved.