Our new Appfire Documentation Space is now live!

Take a look here! If you have any questions please email support@appfire.com

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 61 Next »

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 that you need not create a new condition/post-function/validator for the tutorials. The imported backup has everything in place.

On this page:

2.1 Perform an action based on the value of a field

Let us handle two different cases under this tutorial one to perform an action when the value of the field is equal to a specific value and the another to perform an action when the value of the field is not  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 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 userFor this you will need a Groovy condition in the Scripted (Groovy) condition 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: Process 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.

    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 (.nameto 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-5
  3. Click on Test
  4. The following result will be displayed.

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

2.1.2 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 empty. Imagine a functional use case where you want the field Attachments as required only when the issue type is Bug. For this you will need a Groovy validator in the Scripted (Groovy) validator to check whether the Issue type is Bug or not.

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: Process Management Workflow
  4. Edit it in Diagram mode
  5. Click on transition Create
  6. Go to the Validator tab
  7. Edit the Scripted (Groovy) validator which is the second validator in the list.

Step 2 - Write the script in the validator

  1. Write the following script in Groovy script.

    issue.get("issuetype").name != "Bug"

    Line 1 :  Using != operator to check the name of the Issue type


  2. Your configuration should look like this:

Step 3 - Test your script

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

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. Create a Bug without providing any attachment.
  2. An error message will be displayed, blocking you from creating a Bug without atta 

References

2.2 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 when the priority of an issue is Critical or HighestFor this you will need 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.

Writing the script

Step 1 - Navigate to the post-function

  1. Re-edit the workflow in Diagram mode.
  2. Click on transition Create
  3. Go to the Post-functions tab
  4. 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.

    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 2 :  Using the if-else method to return true if the condition passes and false if it fails


  2. Your script will look like this:

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. 

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

  1. Go to the editor
  2. Replace the script with the following code:

    issue.getAsString("priority") == "Critical" || issue.getAsString("priority") == "Highest"

    Line 1 : Removing the if-else and returning the result of the comparision. 


Step 5 - Retest your script

  1. Click on Test again.
  2. The following result will be displayed. 

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

2.3 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 of the issue based on the impact of the issue specified by the issue creator. For this you will need to write a switch control structure in Value field of the Set field value post-function to set the 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 workflow in Diagram mode.
  3. Click on the Create transition
  4. Click on Post-functions
  5. Edit the third post-function in the list.

Step 2 - Write the script in the post-function

  1. Write the following script in Value.

    switch (issue.getAsString("Impact")) {
     case "Company wide" : return "Highest";
     case "More than one project" : return "High"
     case "Single project" || "Individual" : return "Medium"
      default : return "Medium"
    }

    (2) switch control structure to handle different conditions

    (4) Fall through cases sharing the same code for multiple matches.


  2. Your script will look like this:

Step 3 - Test your script

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

Use cases

You can use this script in any relevant context, such as, setting the priority of the issue to based on the Component/s of the issue.

References

2.4 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 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 satisifed is selected in the Feedback custom field.

Writing the script

Step 1 - Navigate to the post-function

  1. Re-edit the workflow
  2. Click on the Done transition
  3. Click on Post-functions
  4. 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 in the Conditional execution.

    issue.get("Feedback").any {
      it.getValue() == "Not satisfied"}

    Line 1: Using any to return true if any of the element has the specified value

    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-8
  3. Click on Test
  4. 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.5 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 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 Ready for review transition
  3. Click on Validators
  4. Edit the Email issue () post-function in the list.

Step 2 - Write the script in the post-function

  1. Write the following script in the Conditional execution.

    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


  2. Your validator will look like this:

Step 3 - Test your script

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-9
  3. Click on Test
  4. 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. 

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

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

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-5
  3. Click on Test
  4. 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.

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

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

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-5
  3. Click on Test
  4. 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

  • No labels