...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
Excerpt |
---|
Excerpt |
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. |
Note |
---|
Note that you need not create a new condition/post-function/validator for the tutorials. The imported backup has everything in place. |
On this page:
Table of Contents | ||
---|---|---|
|
...
Perform
...
an action when the value of the field is
...
equal to a specific value
...
2.1.1 Perform an action when the value of the field is equal to a specific value
This section of the tutorial guides you through writing a conditional statement that compares the value of a field against a Stringvariable. 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 a Groovy condition in the a Scripted (Groovy) condition with a Groovy script to compare the username of the Assignee and the logged-in user.
Writing the script
Step 1 - Navigate to the condition
Go to the Administration
icon andicon and click on it.
Go to Issues - > Workflows
Locate the workflow GIJ:
ProcessTask Management Workflow
Edit it
Click on
transitiontransition Start Progress
Go to
thethe
tabConditions
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
.Code Block language groovy firstline 1 linenumbers true issue.get("assignee")?.name == currentUser.name
Line 1 : Using the
get()
method to access the assignee of the issue.
Using the
directdirect field access operator (
to.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
5GIJ-
2
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
5GIJ-
2
in a new tab.Check that the transition
for a triggerStart Progress
is not availableon the issue
5GIJ-
2.
Click on
Assign to me
, to assign it to the logged-in user.
Check that the transition
for a triggerStart Progress
is now availableon the issue
5GIJ-
2.
Use cases
You can use this script in any other relevant context, such as allowing allowing only the reporter Reporter of the issue to close the issue.
References
...
Perform an action when the value of the field is not equal to a specific value
This section of the tutorial guides you through writing a conditional statement that compares the value of a field against not being emptyequal to a specific value. Imagine a functional use case where you want the field Attachments as required to allow the creation of an issue only when the issue type is Bugnot assigned to the Reporter. For this, you will need a Groovy validator in the Scripted (Groovy) validator to check whether the Issue type is Bug or not with a Groovy script to compare the Reporter and Assignee.
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
Edit the workflow in
Diagram
mode Click on
transitiontransition Create
Go to
the Validator tabthe Validators tab
Edit the
inScripted (Groovy) validator
which is the second validatoron the list.
Step 2 - Write the script in the validator
Write the following script in
Groovy script
.Code Block language groovy firstline 1 linenumbers true issue.get("issuetypereporter").name != issue.get("Bugassignee")
Line 1 : Using != operator to
checkcompare the
nameReporter and Assignee of the
Issue typeissue
Your configuration should look like this:
Step 3 - Test your script
Click on
Test Groovy Script
.Input the issue key
6GIJ-
1
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
Create
a Bug without providing any attachment.an issue
In the Assignee field, click on Assign to me.
Click on on “Create”
An error message "Reporter and Assignee cannot be the same" will be displayed, blocking you from creating
a Bug without attathe issue.
Clear the Assignee.
Click on Create.
The issue will be created successfully.
References
- Issue type
...
Perform the same action for two different values of a field
This section of the tutorial guides you through writing a condition to test for two different values of a field and perform the same action based on the result. Imagine you want to assign the issue to the Project lead Lead when 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 a Set field value post-function. The condition should return true
if the priority Priority of the issue is either Critical
or Highest
.
Writing the script
Step 1 - Navigate to the post-function
Re-edit the workflow in
Diagram
mode.Click on transition Create
Go to
the Postthe Post-
functions tabfunctions tab
Edit the
Set field value
post-function which is the second post-function in the list.
Step 2 - Write the script in the editor
Write the following script in the editor of the Conditional execution section.
Code Block language groovy linenumbers true if(issue.getAsString("priority") == "Critical" || issue.getAsString("priority") == "Highest"){ return true } else{ return false }
Line 1 : Using the
getAsString()
method to return the String representation of the value.Using the or operator (||) to compare two Strings.
Line
21 & 4 : Using the
if-else
method to returntrue
if the condition passes andfalse
if it failsYour
script willconfiguration should 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
structure and returning the result of the comparison which is a Groovy true or false
Go to the Groovy editor
Replace the script with the following code:
Line 1 : Removing the if-else and returning the result of the comparision.Code Block language groovy linenumbers true issue.getAsString("priority") ==in ["Critical" || issue.getAsString("priority") == "Highest"
,"Highest"]
Step 5 - Retest your script
Click on
Test again
.The following result will be displayed.
Step 6 - Save the configuration
Click on
Update
Publish the workflow (you don't need to save a backup copy).
Use cases
You can use this script in any other relevant context, such as , all newly created issues with Critical
or Highest
priority should transition to the Waiting for approval status instead of Open status.
References
...
Perform different actions for different values of a 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 button field. Imagine you want to set the priority Priority of the issue based on the impact Impact of the issue specified by the issue creator. For this, you will need to write a switch control structure in the Value
field of the Set field value post-function to set the priority Priority based on the selected impactImpact.
Writing the script
Step 1 - Navigate to the post-function
- Go back to the previous tab.
- Edit the
Re-edit the workflow in
Diagram
mode. Click on the Create transition
Click on
Post-functions
tab.Edit the third post-function in the list.
Step 2 - Write the script in the post-function
Write the following script in
Value.
Code Block language groovy switch (issue.getAsString("Impact")) { case "Company wide" : return "Highest"; case "More than one project" : return "High" case "Single project" :
||case "Individual" : return "Medium" default : return "Medium" }
(2) switch Line 1 : Switch control structure to handle different conditions
(Line 4
) : Fall through cases sharing the same code for multiple matches.
Your
script will configuration should look like this:
Step 3 - Test your script
Click on
Test Groovy Script
.Input the issue key
6GIJ-
- Click on
Test
- The following result will be displayed.
3
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).
Use cases
You can use this script in any relevant context, such as, as setting the priority of the issue to based on the Component/s
of the issue.
References
...
Perform an action 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 Lead of the issue when the customer selects Not satisfied
in the feedback form while closing the issue. For this, you will need a condition in the Conditional execution of the Email issue post-function to send an Email only when the option Not
satisifedsatisfied
is selected in the Feedback
custom field.
Writing the script
Step 1 - Navigate to the post-function
Re-edit the workflow
Click on the
DoneReopen
transitionClick
on Poston
Post-functions
tab.Edit the Email issue post-function which is the first in the list.
Step 2 - Write the script in the post-function
Write the following script
in thein the
Conditional execution.
Code Block language groovy issue.get("Feedback").any { it.getValue() == "Not satisfied"}
Line 1: Using
any
to returntrue
if any of the
element has the specified valuespecified option has been selected
Line 2 : Using closure and an implicit parameter (it)
Step 3 - Test your script
Click on
Test Groovy Script
.Input the issue key
8GIJ-
4
Click on
Test
The following
result will be displayed.result will be displayed.
Step 4 - Save the configuration
Click on
Update
Publish the workflow (you don't need to save a backup copy).
Use cases
You can use this script in any relevant context, such as , Escalate the issue only when the issue has been flaggedFlagged.
References
...
Perform an action based on the number of selected elements of a multi-valued field
This section of the tutorial guides you through writing a condition to check the number of selected elements 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 a maximum of three books for rent from a Library Inventory management. For this you will need 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
- Re-edit the workflow.
- Click on the Ready for review transition
- Click on Validators
- 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.
Code Block | ||||
---|---|---|---|---|
| ||||
issue.get("Books").size() >= 1 && issue.get("Books").size() <=3 |
Line 1 : Using size() method to return the number of selected books
&& operator to handle a minimum and maximum value
...
Step 3 - Test your script
- Click on
Test Groovy Script
. - Input the issue key
GIJ-9
- Click on
Test
- The following result will be displayed.
Use cases
You can use this script in any relevant context, such as, Block the user from providing more than one version in the Fix Version/s
References
2.6 Perform an action for values of a multi-valued field that satisfies a condition
This section of the tutorial guides you through performing an action for values of a multi-value field that satusy a condition.
...
Inventory management. For this, you will need 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
Re-edit the workflow.
Click on the
Rent Books
transitionClick on
Validators
tab.Edit the first Scripted (Groovy) validator in the list
Step 2 - Write the script in the post-function
Write the following script in the
Groovy script.
Code Block language groovy issue.get("Books").size() >= 1 && issue.get("Books").size() <=3
Line 1 : Using size() method to return the number of selected books
&& operator to check for a minimum and maximum value
Your configuration should look like this:
Step 3 - Test your script
Click on
Test Groovy Script
.Input the issue key
GIJ-4
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).
Use cases
You can use this script in any relevant context, such as Block the user from providing more than one version in the Fix Version/s and Affects Version/s
References
Perform an action based on the values of two fields
This section of the tutorial guides you through writing a condition to check for the values of two fields and perform an action based on the outcome. Imagine a functional use case where you want to assign the issue to Joe Smith and set the priority to Critical, only when a Bug is created by John Carter. For this, you need to add the Scripted (Groovy) post-function to the Create transition to check the issue type, the reporter, and then assign the issue. You can do this by:
Adding two Set field value post-functions (Recommended)
Expand |
---|
Step 1 - Navigate to the post-function |
...
Step 2 - Write the script in the |
...
conditional execution
|
...
|
...
|
...
|
...
(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 based on the values of two fields
This section of the tutorial guides you through writing a condition to check for values of two fields and perform an action based on the outcome.
...
Step 3 - Navigate to the post-function
Step 4 - Write the script in the conditional execution
Step 5 - Test your script
|
Adding the Scripted Groovy post-function
...
Expand |
---|
Step 1 - Navigate to the post-function |
...
|
...
|
...
|
...
|
...
Step 2 - Write the script in the post-function
|
...
|
...
issue.get("Books").size() > 1 && issue.get("Books").size() <=3
(1) any - returns true
if any element has the specified value
...
Line 1 : Condition checking the values of two fields using the && operator Line 2 and 3 : Using Step 3 - Test your script |
...
|
Use cases
You can use this script implement one of these approaches in any relevant context, such as , Escalate the issue only when the issue has been flagged.
References
...
Set the Severity of the issue based on the priority and resolution of the issue.