User related Groovy script snippets for ScriptRunner for JIRA (Server/Data Center)
Posted by Bastian Wedler on March 20, 2021
Why should I use ScriptRunner?
Using Groovy scripts in Jira Software/Core or Service Management (Server/Data Center) adds a huge amount of new possibilities in customizing workflows, enhancing the user experience and automating processes. The most powerful App in this area is ScriptRunner for Jira. Out of the box it allows you to use Groovy scripts in:
Behaviours (manipulating field behaviour on create, edit and transition screens)
Custom JQL Functions
Fragments (manipulating Jira’s user interface)
and some useful built-in scripts
By default Jira’s data model is quite static. Also you need to be Jira Administrator to modify fields. In combination with other apps you are able to provide a completely new Jira experience. For example, using the Jira app Insight - Asset Management you can define asset objects and access them in your scripts. By that you can make Jira behave more dynamic.
How to use this blog article?
In this article I will share script snippets for ScriptRunner to handle some common user related use cases I got in touch with. I don’t go into the details of the use cases. The purpose of the snippets is to get you started. Use copy & paste at your own discretion.
First of all you import all the packages you’ll need in your script context. For debugging it’s helpful to import org.apache.log4j.Logger classes to be able to write into the Jira logs. You will soon recognize that developing in the integrated ScriptRunner script console without custom logging is quite shitty.
For providing detailed debug information I recommend to use the appropriate log4j log level log.debug(”custom debugging message”). Whether the log.debug statement will appear in the logs, depends on the configured log level of your Jira instance. There is even a webinterface for configuring log levels in Jira Server and Data Center.
Getting the issue object
ScriptRunner scripts can be used in different contexts (listeners, postfunctions, …). How to access the issue object depends on the context of your script. Here some common examples.
Getting the issue object in a listener script
Getting the issue object in script postfunctions
In a ScriptRunner script postfunction you don’t need to fetch the issue object of the current issue. It already exists as a prepopulated variable with name issue. You can directly access it to e.g. get the issue key by using issue.getKey().
Getting the issue object by the issue key
Independent of the context, you can always get the issue object by the issue key.
User related script examples
Now, everything is prepared and you can start using the following script snippets. They’re all tested in Jira 8.5.x (Long Term Support release) with ScriptRunner 6.15.0 and Insight - Asset Management 8.6.15.