Dynamic Forms

Use the Dynamic Forms feature to simplify the process of adding variables to your ScriptRunner Groovy scripts. Dynamic Forms allows you to create complex scripts with flexible variables that can be shared with multiple users, allowing one script to be used for various use cases.

Inline scripts are often copied and pasted, with minor changes made for different use cases. This approach requires maintenance for each usage of the script. Using Dynamic Forms, create flexible scripts with annotated variables that can be stored as files, reducing maintenance requirements while allowing for script customization.

Annotate your variables to have them appear as form fields when a user edits a script. These annotations allow variable values within a script to be changed easily by those with limited code familiarity.

Dynamic forms annotations are available anywhere you can enter code in ScriptRunner, including the Script ConsoleJobs, Listeners, Fragments and many more.

For example, as a power user, you have created a script to delete pages within a space by a specific author. This script is used across multiple locations, with the CQL query varying for each use case. Instead of hardcoding the query variable, you can now use a dynamic form annotation, meaning the query can be changed without needing to re-write the script. As well as being able to edit the script quickly, you can maintain the code in one centralized location. The dynamic form annotation shows the query variable as a form field:

The above example uses the following code:

groovy
import com.atlassian.confluence.api.model.Expansion import com.atlassian.confluence.api.model.content.Content import com.atlassian.confluence.api.model.pagination.PageResponse import com.atlassian.confluence.api.model.pagination.SimplePageRequest import com.atlassian.confluence.api.model.search.SearchContext import com.atlassian.confluence.api.service.search.CQLSearchService import com.atlassian.confluence.core.DefaultDeleteContext import com.atlassian.confluence.pages.PageManager import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput import com.onresolve.scriptrunner.runner.customisers.PluginModule @ShortTextInput(label = "CQL", description = "CQL query to determine which pages to delete") String cqlQuery @PluginModule PageManager pageManager @PluginModule CQLSearchService cqlSearchService def maxResults = 100 def pageRequest = new SimplePageRequest(0, maxResults) def searchResult = cqlSearchService.searchContent(cqlQuery, SearchContext.builder().build(), pageRequest, Expansion.combine("space")) as PageResponse<Content> searchResult.results.collect { result -> def page = pageManager.getPage(result.space.key, result.title) if (page) { pageManager.trashPage(page, DefaultDeleteContext.SUPPRESS_NOTIFICATIONS) return result.title } }

Dynamic form types

The following dynamic form field types are available:

NameDescriptionTarget Type
Short textField allowing a short text inputString
NumberFeud allowing an integerInteger
Select listSingle-select list fieldString
CheckboxA checkbox fieldBoolean
Space pickerField allowing space selection

com.atlassian.confluence.spaces.Space

User pickerField allowing user selectioncom.atlassian.confluence.User

Interested in more dynamic forms types? Raise a support request here

Create a dynamic form

  1. Navigate to ScriptRunner.

  2. Select Console.

  3. Write your new script in the Script field, annotating the variables you want users to provide.
    Annotating variables allows them to be edited depending on requirements when running the script. These variables show as editable fields above the Script Console.

Transforming an existing inline script

To enable sharing of annotated scripts, all inline scripts must be saved as files.

  1. Navigate to your existing inline script, and add in required annotations (below).

  2. Copy the script.

  3. Use the Script Editor to open your Scripts Root folder.

  4. Select the folder in which you want to save the script, and click the Create New File icon.

  5. Enter a file name in the Add New Groovy File/Add New Groovy Folder window.

  6. Click Add.

  7. Paste your inline script into the file, and click Save. This script is now available as a file and can be shared with multiple Jira users on the same instance.

Annotations 

Short text

Add a short text field to a script

groovy
import com.onresolve.scriptrunner.parameters.annotation.* @ShortTextInput(label = "Page name", description = "Enter a name for your page") String pageNameTextInput

Number

Add a number field to a script

groovy
import com.onresolve.scriptrunner.parameters.annotation.NumberInput @NumberInput(label = "Number of child pages", description = "How many child pages to create") Integer numberOfChildPages

Select list

Add a single-select list with configurable options

groovy
import com.onresolve.scriptrunner.parameters.annotation.* import com.onresolve.scriptrunner.parameters.annotation.meta.* @Select( label = "Color", description = "Select color", options = [ @Option(label = "Green", value = "green"), @Option(label = "Blue", value = "blue"), ] ) String value

Checkbox

Add a checkbox to the script

groovy
import com.onresolve.scriptrunner.parameters.annotation.* @Checkbox(label = "Clone space", description = "Select the checkbox to clone a space") Boolean spaceShouldBeCloned

User picker

Add a user picker into your script

groovy
import com.atlassian.user.User import com.onresolve.scriptrunner.parameters.annotation.* @UserPicker(label = "User", description = "User for test") User user

Add a user multi-picker

groovy
import com.atlassian.user.User import com.onresolve.scriptrunner.parameters.annotation.* @UserPicker(label = "Users", description = "Users for test", multiple = true) List<User> users

Space picker 

Add a space picker to your script

groovy
import com.atlassian.confluence.spaces.Space import com.onresolve.scriptrunner.parameters.annotation.SpacePicker @SpacePicker(label = "Space", description = "Pick a space") Space space

Add a space multi-picker to your script

groovy
import com.atlassian.confluence.spaces.Space import com.onresolve.scriptrunner.parameters.annotation.SpacePicker @SpacePicker(label = "Spaces", description = "Pick spaces", multiple = true) List<Space> spaces
On this page