Excerpt |
---|
When you access the field of an issue, you might need to verify that it is not |
On this page:
Table of Contents maxLevel 2
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
...
Step 2 - Write the script in the editor
Write the following script in the editor.
Code Block language groovy linenumbers true 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
Code Block language groovy linenumbers true 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
Code Block language groovy linenumbers true 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
...
Step 1 - Write the script in the editor
Write the following script in the editor.
Code Block language groovy linenumbers true 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
Code Block language groovy linenumbers true 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
Code Block language groovy linenumbers true 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.
Code Block language groovy linenumbers true 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
Code Block language groovy linenumbers true 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
Code Block language groovy linenumbers true issue.get("versions")?.first()?.releaseDate
Step 6 - Retest your script
- Click on
Test again
. - The following result will be displayed