howto call a groovy code from Scriptrunner for Confluence macro

I want to write a macro with Scriptrunner. It should place a button over a table. On pressing that button i want to read some of the fields of that table and send it to a REST Service.

I know how to place that button from the examples provided from Adaptavist. I also know how to write groovy code to have a function to feed the REST Service. 

I struggle where to put the groovy code, so that I can call it, when the button is pressed. Can sonone provide an example, please?


1 answer

So, there are a few things you might be asking; I'll try to address each.

Per the documentation on custom macros, there are three code blocks that define your macro.

The first takes some Groovy code that should return an HTML string. The HTML will be what ultimately renders on the page, and in your case, should probably include an <button> tag.

The second takes some JavaScript code. This is where you'd write the code that read the contents of your table and sent the data to an external REST service.

The third block is where you can put CSS, to style your macro.

If you're asking where you put the code for your REST Service, that would be in a custom REST Endpoint, which is a separate piece of configuration from your macro.

Does that make sense?

To expand a bit, the javascript to read your table might look something like this:

AJS.toInit(function () {
let buttonSelector = 'button#myCustomMacroButton';
let button = document.querySelector(buttonSelector);

function clickHandler(clickEvent) {
let tableElements = document.querySelectorAll(buttonSelector + ' ~ table td');
let tableData = [];
tableElements.forEach(function (tdElement) {
type: "POST",
url : AJS.params.contextPath + "/rest/scriptrunner/latest/custom/myCustomEndpoint",
data: {
tableData: tableData
}).done(function (data, status, jqXhr) {
console.log("Retrieved response from custom rest endpoint: ");

button.addEventListener('click', clickHandler);

The handler function passed to "done" is what happens whenever your data comes back from the server. I assume you'd want something more than just logging the data to the console, but that's just an example.

Your REST Endpoint would need to receive that data and do something with it.

import groovy.json.JsonBuilder
import groovy.transform.BaseScript

@BaseScript CustomEndpointDelegate delegate

httpMethod: "POST", groups: ["confluence-administrators"]
) { MultivaluedMap queryParams, String body ->

def mapper = new ObjectMapper()
def data = mapper.readValue(body, Map)

return Response.ok("Received data: $data").build()

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 12, 2019 in Confluence

Confluence Admin Certification now $150 for Community Members

More and more people are building their careers with Atlassian, and we want you to be at the front of this wave! Important Dates Start the Certification Prep Course by 2 April 2019 Take your e...

360 views 2 13
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you