Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

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. 

On this page:

Table of Contents
maxLevel1

...

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
maxLevel1

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 variable. 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 userFor this, you will need 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

  1. Go to the Administration icon and click on it.

  2. Go to Issues - > Workflows

  3. Locate the workflow GIJ: Task Management Workflow

  4. Edit it

  5. Click on transition Start Progress

  6. Go to the Conditions tab

  7. Edit the Scripted (Groovy) condition which is the first condition in the list.

Step 2 - Write the script in the condition

  1. Write the following script in Groovy script.

    Code Block
    languagegroovy
    issue.get("assignee")?.name == currentUser.name

    Line 1 :  Using the get() method to access the assignee of the issue. 

                  Using the direct field access operator (.name) to retrieve the field instead of calling the getter (getName())

  2. Your configuration should look like this:

Step 3 - Test your script

  1. Click on Test Groovy Script.

  2. Input the issue key GIJ-2

  3. Click on Test

  4. The following result will be displayed.

  5. Image Added

Step 4 - Save the configuration

  1. Click on Update

  2. Publish the workflow (you don't need to save a backup copy).

Step 5 - Test it on an issue

  1. View the issue GIJ-2 in a new tab.

  2. Check that the transition Start Progress is not available on the issue GIJ-2.

  3. Click on Assign to me, to assign it to the logged-in user

  4. Check that the transition Start Progress is now available on the issue GIJ-2. 

Use cases

You can use this script in any other relevant context, such as allowing only the 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 a String not being equal to a specific value. Imagine a functional use case where you want to allow the progress on creation of an issue only when the issue is not assigned to the logged in userReporter. For this, you will need a Groovy condition in the Scripted (Groovy) conditionvalidator with a Groovy script to compare the usernames of the Assignee and the logged in user. 

...

the Reporter and Assignee.

Writing the script

Step 1 - Navigate to the condition

  1. Go to the Administration icon Image Removed and click on it.
  2. Go to Issues - > Workflows
  3. Locate the workflow GIJ: Process Management Workflow
  4. Edit it
  5. Click on transition Start Progress
  6. Go to the Conditions tab

    Edit the workflow in Diagram mode

  7. Click on transition Create

  8. Go to the Validators tab

  9. Edit the Scripted (Groovy)

    condition

    validator which is the

    first condition in

    second validator on the list.

Step 2 - Write the script in the

...

validator

  1. Write the following script in

     Groovy

     Groovy script.

    Code Block
    languagegroovy
    firstline1
    linenumberstrue
    issue.get("assigneereporter").name !== currentUser.name
    (1)  get() method to access the assignee. Direct field access operator (.nameto retrieve the field instead of calling the getter (getName()). 
     issue.get("assignee")

    Line 1 :  Using != operator to compare the Reporter and Assignee of the issue

  2. Your configuration should look like this:

    Image Added

Step 3 - Test your script

  1. Click on Test Groovy Script.

  2. Input the issue key GIJ-

    5

    1

  3. Click on Test

  4. The following result will be displayed.

  5. Image RemovedImage Added

Step 4 - Save the configuration

  1. Click on Update

  2. Publish the workflow (you don't need

    to save a backup copy).

Step 5 - Test it on an issue

  1. View the issue GIJ-5 in a new tab.
  2. Check that the transition Start Progress is not available for a trigger on the issue GIJ-5.
  3. Click on Assign to me, to assign it to the logged in user.
  4. Check that the transition Start Progress is now available for a trigger on the issue GIJ-5. 

Use cases

You can use this script in any other relevant context, such as allowing only the reporter of the issue to close the issue.

References

...

  1. to save a backup copy).

Step 5 - Test it on an issue

  1. Create an issue

  2. In the Assignee field, click on Assign to me.

  3. Click on on “Create”

  4. An error message "Reporter and Assignee cannot be the same" will be displayed, blocking you from creating the issue.

  5. Clear the Assignee.

  6. Click on Create.

  7. The issue will be created successfully.

References

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 if Lead when the priority of an issue is Critical or HighestFor 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 Priority of the issue is either Critical or Highest.

Writing the script

Step 1 - Navigate to the post-function

  1. Go to the Administration icon Image Removed and click on it.
  2. Go to Issues - > Workflows
  3. Locate the workflow GIJ: Process Management Workflow
  4. Edit it
  5. Click on transition Create
  6. Go to the Post-functions tab

    Re-edit the workflow in Diagram mode.

  7. Click on transition Create

  8. Go to the Post-functions tab

  9. Edit the Set field value post-function which is the

    ()

    second post-function in the list.

Step 2 - Write the script in the editor

  1. Write the following script in the editor of the Conditional execution section.

    Code Block
    linenumbers
    languagegroovytrue
    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 (||

    operator

    ) to compare two Strings. 

    (2)

    Line 1 & 4 :  Using the if-else method to return true if the condition passes and false if it fails

  2. Your

    script will

    configuration should look like this:

    Image Added

Step 3 - Test your script

  1. Click on Test Groovy Script.

  2. Input the issue key GIJ-3

  3. Click on Test

  4. The following result will be displayed. 

  5. Image RemovedImage Added

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 truthy true or falsyfalse

  1. Go to the Groovy editor

  2. Replace the script with the following code:

    Code Block
    languagegroovy
    linenumberstrue
    issue.getAsString("priority") == "Critical" || issue.getAsString("priority") == "Highest"
    (1) Groovy truth - Returns true if the corresponding Boolean value is true.
    in ["Critical","Highest"]

Step 5 - Retest your script

  1. Click on Test again.

  2. The following result will be displayed. 

    Image Removed

  3. Image Added

Step 6 - Save the configuration

  1. Click on Update

  2. 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 new Critical and all newly created issues with Critical or Highest priority issues should directly go transition to the Waiting for approval status when the issue is created 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 buttons 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 Impact.

Writing the script

Step 1 - Navigate to the post-function

  1. Go back to the previous tab.
  2. Edit the

    Re-edit the workflow in Diagram mode.

  3. Click on the Create transition

  4. Click on Post-functions tab.

  5. Edit the

    second

    third post-function in the list.

Step 2 - Write the script in the post-function

    def impact =
  1. Write the following script in Value.

    Code Block
    languagegroovy
  2. linenumberstrue
  3. switch (issue.getAsString("
  4. impact
  5. Impact")
  6. switch (impact
  7. ) {
     case "Company wide" : return "Highest";
  8. break
  9. 
     case "More than one project" : return "High"
    
  10. ;
  11.  
  12. break
  13. case "Single project" : 
  14. ||
  15. case "Individual" : return "Medium"
    
  16. ;
  17.  
  18. break
  19.  
  20. default : return "Medium"
  21. ;
  22. 
    
  23. break
  24. }
  25. (2) switch
  26. Line 1 : Switch control structure to handle different

  27. conditions

    (3) break statement - to exit the switch statement

    (4) 
  28. conditions

    Line 4 : Fall through cases sharing the same code for multiple matches.

  29. Your configuration should look like this:

    Image Added

Step 3 - Test your script

  1. Click on Test Groovy Script.

  2. Input the issue key GIJ-

    5

    3

  3. Click on Test

  4. The following result will be displayed.

    Image Removed

  5. Image Added

Step 4 - Save the configuration

  1. Click on Update

  2. 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

  1. Go back to the previous tab.
  2. Edit the workflow in Diagram mode.

    Re-edit the workflow

  3. Click on the

    Close transition

    Reopen transition

  4. Click

    on Post

    on Post-functions tab.

  5. Edit the Email issue

    ()

    post-function which is the first in the list.

Step 2 - Write the script in the post-function

  1. Write the following script

  2. in the
  3. in the Conditional execution.

    Code Block
    languagegroovy
  4. linenumberstrue
  5. issue.get("Feedback").any {
      it.getValue() == "Not satisfied"}
  6. (
  7. Line 1

  8. )
  9. : Using any

  10. -
  11. to return 

  12. returns
  13. true if any

  14. element has
  15. the specified

  16. value(2) A closure using
  17. option has been selected

    Line 2 : Using closure and an implicit parameter (it)

Step 3 - Test your script

  1. Click on Test Groovy Script.

  2. Input the issue key GIJ-

    5

    4

  3. Click on Test

  4. The following result will be displayed.

    Image Removed

  5. Image Added

Step 4 - Save the configuration

  1. Click on Update

  2. 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 size 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

  1. Re-edit the workflow.

  2. Click on the Rent Books transition

  3. Click on Validators tab.

  4. Edit the first Scripted (Groovy)

...

Writing the script

Step 1 - Navigate to the post-function

  1. Go back to the previous tab.
  2. Edit the workflow in Diagram mode.
  3. Click on the Rent transition
  4. Click on Post-functions
  5. 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
languagegroovy
linenumberstrue
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

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-5
  3. Click on Test
  4. The following result will be displayed.
  5. Image Removed

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 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. 

...

  1. validator in the list

Step 2 - Write the script in the post-function

  1. Write the following script in the Groovy script.

    Code Block
    languagegroovy
    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

  2. Your configuration should look like this:

    Image Added

Step 3 - Test your script

  1. Click on Test Groovy Script.

  2. Input the issue key GIJ-4

  3. Click on Test

  4. The following result will be displayed.

  5. Image Added

Step 4 - Save the configuration

  1. Click on Update

  2. 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

...

  1. Re-edit the workflow.

  2. Click on the

...

  1. Create transition

  2. Click

...

  1. on Post-functions

...

  1. tab.

  2. Add a Set field value post-function.

  3. Select the "Assignee" field

  4. Select "Groovy template" as the "Value Type"

  5. Input the following in the Value field:

    Code Block
    jsmith

Step 2 - Write the script in the

...

conditional execution

  1. Select "Conditional Execution" option

  2. Write the following script

...

  1. in the Condition.

    Code Block
    languagegroovy

...

  1. issue.

...

  1. issuetype.name == "Bug" && issue.

...

(1) any - returns true if any element has the specified value

(2) A closure using an implicit parameter (it)

Step 3 - Test your script

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-5
  3. Click on Test
  4. The following result will be displayed.
  5. Image Removed

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.

...

  1. reporter.name == "jcarter"

    Line 1 : Condition checking the values of two fields using the && operator

Step 3 - Navigate to the post-function

  1. Add a Set field value post-function.

  2. Select the "Priority" field

  3. Select "Groovy template" as the "Value Type"

  4. Input the following in the Value field:

    Code Block
    Highest

Step 4 - Write the script in the conditional execution

  1. Select "Conditional Execution" option

  2. Write the following script in the Condition.

    Code Block
    languagegroovy
    issue.issuetype.name == "Bug" && issue.reporter.name == "jcarter"

    Line 1 : Condition checking the values of two fields using the && operator

Step 5 - Test your script

  1. Click on Update.

  2. Publish the workflow (you need to take a back up)

  3. Create a Bug in GIJ project.

  4. Check the Assignee and Priority of the issue after the issue creation.

Adding the Scripted Groovy post-function

Expand

Step 1 - Navigate to the post

...

-function

  1. Re-edit the workflow.

  2. Click on the

...

  1. Create transition

  2. Click

...

  1. on Post-functions tab.

  2. Edit the

...

  1. fourth post-function

...

  1. on the list.

Step 2 - Write the script in the post-function

  1. Write the following script

...

  1. in the Groovy script.

    Code Block
    languagegroovy

...

issue.get("Books").size() > 1 && issue.get("Books").size() <=3

(1) any - returns true if any element has the specified value

...

  1. if(issue.issuetype.name == "Bug" && issue.reporter.name == "jcarter"){
      issue.setFieldValue("assignee","jsmith")
      issue.setFieldValue("priority","Highest")
    }

Line 1 : Condition checking the values of two fields using the && operator

Line 2 and 3 : Using setFieldValue method of the Issue interface to set a field value

Step 3 - Test your script

  1. Click

...

  1. on Update.

  2. Publish the workflow (you need to take a back up)

  3. Create a Bug in GIJ project.

  4. Check the assignee of the issue and the priority after the issue creation.

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.

References