Writing your first Groovy script

This tutorial will guide you through writing your first Groovy script. 

Let us write a simple Groovy script for the functional use case: Store the author who approved an issue into a User picker custom field

You can do this by adding the Set field value post-function to the transition that should be triggered to approve the ticket, write a Groovy script in the post-function configuration to return an ApplicationUser object representing the logged in user and then set the value of the User picker custom field to the result of the written Groovy script. 

On this page:


You need not create a user picker field or configure a post-function for this. The imported backup has everything in place.

Writing the script

Step 1 - Navigate to the post-function

  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 Approve
  6. Go to the Post-functions tab
  7. Edit the Set field value post-function which is the first post-function in the list.

Step 2 - Write the script in the post-function

  1. Write the following script in Value 

    currentUser

    Line 1 :  Using the currentUser variable that holds an ApplicationUser representing the current user (i.e. the user triggering the transition) in the Groovy context. 


  2. Your post-function should look like this:

Step 3 - Test your script

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-1
  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. Go to the issue GIJ-1
  2. Click on the transition Approve.
  3. Verify that the Author who approved field under People gets updated with John Carter.

Use cases

You can use this script in any other relevant context, such as

  • Assigning the issue to the current user
  • Adding the current user to the Request Participants of the issue

References

Next >> Safe access to a field