Our new Appfire Documentation Space is now live!
Take a look here! If you have any questions please email support@appfire.com
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
- Go to the Administration icon and click on it.
- Locate Add-ons from the menu and click on it.
- Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
- Click on Groovy script tester.
Step 2 - Write the script in the editor
Write the following script in the editor.
issue.get("assignee").name
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").name }
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")?.name
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 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
Write the following script in the editor.
issue.get("comment").last()
Step 2 - Test your script
- Click on
Test again
. - The following result will be displayed.
Step 3 - Try the Safe navigation operator
- Click on the editor.
Replace the script with the following script
issue.get("comment")?.last()
Step 4 - 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 5 - 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() }
Step 6 - 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 access the Release date of the first Affects Version/s.
Step 1 - Write the script in the editor
Write the following script in the editor.
issue.get("versions").first().releaseDate
Step 2 - Test your script
- Click on
Test again
. - The following result will be displayed.
Step 3 - 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 4 - Retest your script
- Click on
Test again
. - 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
- Click on the editor.
Replace the script with the following script
issue.get("versions")?.first()?.releaseDate
Step 6 - Retest your script
- Click on
Test again
. - The following result will be displayed