Introduction

Some Groovy knowledge required. With the introduction of the new version of Create Page for ScriptRunner Confluence, we allow users the ability to customise macro variables.

We have introduced variables such as $fullname which can be used in the macro. When the page is rendered $fullname will be resolved to the users full name as specified in Confluence. By using ScriptRunner for Confluence it is possible to change the behaviour of $fullname or simply to specify your own variable. For example, you could create a variable $lastCommentDate which could resolve to the date of the last comment on the page.

Instructions

We will be looking at a simple Create Page macro, and how to change the stock custom variables used. Supplied variable are $self, $parent, $username, $fullname, $year, $month, $day, $parenttitle, $pagetitle, $ident.

  • Navigate to your script root. Default is <Confluence>/home/scripts. More information about script roots.

  • Create the package com.onresolve.scriptrunner.canned.confluence.macros in your script root.

  • Navigate to the package specified above and inside the macros folder create a new groovy class "CreatePageMacroModified.groovy" that extends the old class and overrides the setCustomVariables method. A descriptive class name is recommended.

  • Disable the old macro in the Script Macros section in Confluence. Enable the new macro in the same section. The names of the macros will be the same as to not break any macros already used.

custom var tutorial script macro
  • Populate the class:

package com.onresolve.scriptrunner.canned.confluence.macros

import java.time.Instant

/**
 * Overrides custom variables in parent to specify new variable behaviour
 *
 */
class CreatePageMacroModified extends CreatePageMacro {

    @Override
    protected Map<String, String> setCustomVariables() {
        Map<String, String> customVariables = new HashMap<>()
        customVariables.put("\$myVariable", "This is my custom variable")
        customVariables.put("\$epoch", String.valueOf(Instant.now().toEpochMilli()))

        return customVariables
    }
}
  • Use the Create Page macro on a page and edit it with the configured variables

custom var tutorial edit macro
  • Notice the title of the newly created page when using the Create Page macro

custom var tutorial result

Macro class names that are extendable

  • CreatePageMacro

Have questions? Visit the Atlassian Community to connect, share, and learn with other Atlassian users and experts, including Adaptavist staff.

Ask a question about ScriptRunner for JIRA, Bitbucket Server, or Confluence.

Want to learn more? Check out courses on Adaptavist Learn, an online platform to onboard and train new users for Atlassian solutions.