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 display the Email address of the Assignee of the issue in a Calculated text field. Let us write a script for the same in the Groovy formula of the field configuration.
Step 1 - Navigate to the Groovy formula in the field configuration
- Go to the Administration icon and click on it.
- Go to Issues - > Custom fields
- Locate the custom field Email address of Assignee
- Click on the cog wheel and click on
Configure.
- Click on
Edit Groovy Formula.
Step 2 - Write the script in the editor
Write the following script in the editor.
issue.get("assignee").emailAddress
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-1
- Click on
Test
- 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
- Click on the editor.
Replace the script with the following script
if(issue.get("assignee")){ issue.get("assignee").emailAddress }
Step 2 - Retest your script
- Click on
Test again
. - 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
- Click on the editor.
Replace the script with the following script
issue.get("assignee")?.emailAddress
Step 2 - Retest your script
- Click on
Test again
. - 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 show the last comment on an issue in a Calculated text field and there are no comments for the issue you will face a java.util.NoSuchElementException
.
Step 1 - Navigate to the Groovy formula in the field configuration
- Go to the Administration icon and click on it.
- Go to Issues - > Custom fields
- Locate the custom field Last comment on the issue
- Click on the cog wheel and click on
Configure.
- Click on
Edit Groovy Formula.
Step 2 - Write the script in the editor
Write the following script in the editor.
issue.get("comment").last().body
Step 3 - Test your script
- Click on
Test again
. - The following result will be displayed.
Step 4 - Try the Safe navigation operator
- Click on the editor.
Replace the script with the following script
issue.get("comment")?.last().body
Step 5 - Retest your script
- Click on
Test again
. - The same result will be displayed because using the Safe navigation operator does not fix the issue.
Step 6 - Fix your script using the if structure
- Click on the editor.
Replace the script with the following script
if(issue.get("comment")){ issue.get("comment").last().body }
Step 7 - Retest your script
- Click on
Test again
. - 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 display the Release date of the only Affects Version/s on a Calculated Date/Time field.
Step 1 - Navigate to the Groovy formula in the field configuration
- Go to the Administration icon and click on it.
- Go to Issues - > Custom fields
- Locate the custom field Release date
- Click on the cog wheel and click on
Configure.
- Click on
Edit Groovy Formula.
Step 2 - Write the script in the editor
Write the following script in the editor
issue.get("versions").first().releaseDate
Step 3 - Test your script
- Click on
Test again
. - The following result will be displayed.
Step 4 - Apply the Safe navigation operator to the first() method
- Click on the editor.
Replace the script with the following script
issue.get("versions")?.first().releaseDate
Step 5 - Retest your script
- Click on
Test again
. - The following result will be displayed because you cannot get
releaseDate
on a null object
Step 6 - Apply the safe navigation operator to the releaseDate
- Click on the editor.
Replace the script with the following script
issue.get("versions")?.first()?.releaseDate
Step 7 - Retest your script
- Click on
Test again
. - The following result will be displayed