This tutorial will guide you through writing Groovy scripts that access a field of an issue, compare it with another value and perform an action based on the outcome.
On this page:
2.1 Perform an action for a specific value of a field
This section of the tutorial guides you through writing a conditional statement that compares the value of a field against a String. Imagine a functional use case where you want to allow the progress on an issue only when the issue is assigned to the logged in user. For this you will need to write a Groovy condition in the Scripted (Groovy) condition to compare the usernames of the Assignee and the logged in user.
Note that you need not create a new condition for this. The imported backup has everything in place.
Writing the script
Step 1 - Navigate to the condition
- Go to the Administration icon and click on it.
- Go to Issues - > Workflows
- Locate the workflow GIJ: Process Management Workflow
- Edit it
- Click on transition Start Progress
- Go to the
Conditions
tab - Edit the Scripted (Groovy) condition which is the first condition in the list.
Step 2 - Write the script in the condition
Write the following script in
Groovy script
.issue.get("assignee").name == currentUser.name
(1)
get()
method to access the assignee. Direct field access operator (.name
) to retrieve the field instead of calling the getter (getName()
).
Your configuration should look like this:
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-5
- Click on
Test
- The following result will be displayed.
Step 4 - Save the configuration
- Click on
Update
- Publish the workflow (you don't need to save a backup copy).
Step 5 - Test it on an issue
- View the issue
GIJ-5
in a new tab. - Check that the transition
Start Progress
is not available for a trigger on the issueGIJ-5.
- Click on
Assign to me
, to assign it to the logged in user.
- Check that the transition
Start Progress
is now available for a trigger on the issueGIJ-5.
Use cases
The output of the code is a Boolean. You can use this script in any other relevant context, such as allowing only the reporter of the issue to close the issue.
References
2.2 Perform same action for two different values of the same field
This section of the tutorial guides you through writing a condition to check for two different values of a field and peform the same action for both based on the result. Imagine you want to assign the issue to the Project lead if the priority of an issue is Critical
or Highest
. For this you will need to write a Groovy condition in the Conditional execution section of the Set field value post-function. The condition should return true if the priority of the issue is either Critical or Highest. Based on the outcome assign the issue to the Project lead.
Writing the script
Step 1 - Navigate to the post-function
- Go to the Administration icon and click on it.
- Go to Issues - > Workflows
- Locate the workflow GIJ: Process Management Workflow
- Edit it
- Click on transition Create
- Go to the Post-functions tab
- Edit the Set field value post-function which is the () post-function in the list.
Step 2 - Write the script in the editor
Write the following script in the editor of Conditional execution.
if(issue.getAsString("priority") == "Critical" || issue.getAsString("priority") == "Highest"){ return true } else{ return false }
(1)
getAsString()
method to return the String representation of the value. || operator to compare two Strings.(2)
if-else
method to return true if the condition passes and false if it fails
Your script will look like this:
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-3
- Click on
Test
- The following result will be displayed.
Step 4 - Simplify your script
You can simplify the script removing the if-else
and returning the result of the comparison which is a Groovy truthy or falsy
- Go to the editor
Replace the script with the following code:
issue.getAsString("priority") == "Critical" || issue.getAsString("priority") == "Highest"
(1)
Groovy truth
- Returnst
rue
if the corresponding Boolean value istrue.
Step 5 - Retest your script
- Click on
Test again
. - The following result will be displayed.
Use cases
You can use this script in any relevant context, such as, All new Critical and Highest priority issues should directly go to the Waiting for approval status when the issue is created instead of Open status.
References
2.3 Perform different actions for different values of the same field
This section of the tutorial guides you through writing multiple conditions to perform different actions based on different values of a single-select or a Radio buttons field. Imagine you want to set the priority of the issue based on the impact of the issue mentioned by the issue creator. For this you will need to write a switch control structure in the Value of Set field value post-function to pass priority based on the selected Impact.
Writing the script
Step 1 - Navigate to the post-function
- Go back to the previous tab.
- Edit the workflow in
Diagram
mode. - Click on the Create transition
- Click on Post-functions
- Edit the second post-function in the list.
Step 2 - Write the script in the post-function
Write the following script in
Value.
def impact = issue.getAsString("impact") switch (impact) { case "Company wide" : "Highest"; break case "More than one project" : "High" ; break case "Single project" || "Individual" : "Medium" ; break default : "Medium"; break }
(2) switch control structure to handle different conditions
(3) break statement - to exit the switch statement
(4) Fall through cases sharing the same code for multiple matches.
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-5
- Click on
Test
- The following result will be displayed.
Use cases
The output of the code is a String representing the priority of an issue. You can use this script in any relevant context, such as, setting the priority of the issue to based on the Component/s.
References
2.4 Perform an action testing for a specific value of a multi-valued field
This section of the tutorial guides you through writing a condition to check for a specific value of a multi-valued field and perform an action based on the outcome. Imagine you want to send an Email notification to the Project lead of the issue when the customer selects Not satisfied
in the feedback form while closing the issue. For this you will need to write a condition in the Conditional execution of the Email issue post-function to send an Email only when the option Not satisifed is selected in the Feedback custom field.
Writing the script
Step 1 - Navigate to the post-function
- Go back to the previous tab.
- Edit the workflow in
Diagram
mode. - Click on the Close transition
- Click on Post-functions
- Edit the Email issue () post-function in the list.
Step 2 - Write the script in the post-function
Write the following script in the
Conditional execution.
issue.get("Feedback").any { it.getValue() == "Not satisfied"}
(1) any - returns
true
if any element has the specified value(2) A closure using an implicit parameter (it)
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-5
- Click on
Test
- The following result will be displayed.
Use cases
You can use this script in any relevant context, such as, Escalate the issue only when the issue has been flagged.
References
2.6 Perform action for multiple values of a multi-valued field
This section of the tutorial guides you through writing a condition to check for multiple values of a multi-valued field and perform an action based on the outcome. Imagine you want to limit a user to select a minimum of one and maximum of three books for rent from a Library Inventory management. For this you will need to write a condition in the Scripted (Groovy) Validator to count the number of options selected in the Books field on the Rent Books form while renting the books.
Writing the script
Step 1 - Navigate to the post-function
- Go back to the previous tab.
- Edit the workflow in
Diagram
mode. - Click on the Rent transition
- Click on Post-functions
- Edit the Email issue () post-function in the list.
Step 2 - Write the script in the post-function
Write the following script in the
Conditional execution.
issue.get("Books").size() > 1 && issue.get("Books").size() <=3
(1) any - returns
true
if any element has the specified value(2) A closure using an implicit parameter (it)
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-5
- Click on
Test
- The following result will be displayed.
Use cases
You can use this script in any relevant context, such as, Escalate the issue only when the issue has been flagged.
References
2.6 Perform an action for each value of a multi-valued field
This section of the tutorial guides you through writing a condition to check for multiple values of a multi-valued field and perform an action based on the outcome. Imagine you want to limit a user to select a minimum of one and maximum of three books for rent from a Library Inventory management. For this you will need to write a condition in the Scripted (Groovy) Validator to count the number of options selected in the Books field on the Rent Books form while renting the books.
Writing the script
Step 1 - Navigate to the post-function
- Go back to the previous tab.
- Edit the workflow in
Diagram
mode. - Click on the Rent transition
- Click on Post-functions
- Edit the Email issue () post-function in the list.
Step 2 - Write the script in the post-function
Write the following script in the
Conditional execution.
issue.get("Books").size() > 1 && issue.get("Books").size() <=3
(1) any - returns
true
if any element has the specified value(2) A closure using an implicit parameter (it)
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-5
- Click on
Test
- The following result will be displayed.
Use cases
You can use this script in any relevant context, such as, Escalate the issue only when the issue has been flagged.