Can I use a variable with a macro? I am trying to create a custom template and use variables and a JIRA macro together.
Asked by Roland:

I created a custom template.

When in use you enter variables and with that information, I would like these variables be used in a JIRA macro.

I want to list JIRA tickets based on user varables.

an example

from the template I enter variables

2014.12.11 and ECDEV

I want to be able to input those variables into a JIRA Macro.

right now i can get the template to create this URL: <https://trinet.atlassian.net/issues/?jql=project%3DECDEV%20and%20fixVersion%3D>2014.12.11

but it would be nice instead of the URL it creates a table of the results instead

any help would be appreciated


Answers:
author: Adam Barylak
body: I figured since we solved this recently, i would add our official solution to this request which allows to inject any number of variables into any JQL filter in a Confluence Template. Below is the how-to as well as some usage notes i wrote for our company:

First we start out with determining the current syntax of the JQL Issues/Filter macro in confluence:

1. Install or Enabled the "Confluence Source Editor" plugin
2. Create new page
3. Add the JIRA Issue/Filter macro
4. Use a simple basic query
5. Click the "\<\>" button to view the source of the page.
6. Copy the JIRA Issue/Filter macro syntax to a text editor, e.g.

   ```
   &lt;p&gt;  &lt;ac:structured-macro ac:macro-id="5a45704b-a904-4440-bdc0-da68a22d5802" ac:name="jira" ac:schema-version="1"&gt;    &lt;ac:parameter ac:name="server"&gt;JIRA&lt;/ac:parameter&gt;    &lt;ac:parameter ac:name="columns"&gt;key,summary,type,created&lt;/ac:parameter&gt;    &lt;ac:parameter ac:name="maximumIssues"&gt;20&lt;/ac:parameter&gt;    &lt;ac:parameter ac:name="jqlQuery"&gt;assignee = abarylak and resolution is EMPTY &lt;/ac:parameter&gt;    &lt;ac:parameter ac:name="serverId"&gt;c8001771-0b52-30e0-9703-562a2820710f&lt;/ac:parameter&gt;  &lt;/ac:structured-macro&gt;&lt;/p&gt;
   ```

Then we need to use the above syntax (a little modified) and use that for our macro.

1. Create a macro with 2 parameters: maxRows, columns
2. Remove the macro-id from the above syntax
3. Modify the $body variable to remove unnecessary white space using trim() and html paragraph tags (see below)
4. Inject the $body into the jqlQuery element in the above syntax
5. **NOTE:**The "serverId" parameter may change server to server, so you must get that ID from your current server you want to deploy this to via the source editor plugin.
6. Results are as follows:

   ```
   ## Macro title: JQL Filter Allowing Variables## Macro has a body: Y## Body processing: Rendered## Output: JIRA Issues/Filter Macro w/ injected variables#### Developed by: Adam Barylak## Date created: 2016-05-27## Installed by: Adam Barylak## This macro will prompt for max number of rows and the column option, then the body will require the JQL with injected variables## @param maxRows:title=Max Rows|type=int|required=true|default=20|desc=Enter the max rows, anything above 1000 will only display 1000 rows.## @param columns:title=Columns|type=string|required=true|default=key,summary,type,status,assignee|desc=Enter  the columns you want returned.  Must be separated by commas, all lowercase, and no quotes. e.g. key,summary,assignee,test author,reporter#set ($body = $body.replaceAll("\Q&lt;p&gt;\E",""))#set ($body = $body.replaceAll("\Q&lt;/p&gt;\E",""))#set ($body = $body.trim())&lt;p&gt;&lt;ac:structured-macro ac:name="jira" ac:schema-version="1"&gt;    &lt;ac:parameter ac:name="server"&gt;JIRA&lt;/ac:parameter&gt;    &lt;ac:parameter ac:name="columns"&gt;$paramcolumns&lt;/ac:parameter&gt;    &lt;ac:parameter ac:name="maximumIssues"&gt;$parammaxRows&lt;/ac:parameter&gt;    &lt;ac:parameter ac:name="jqlQuery"&gt;$body&lt;/ac:parameter&gt;    &lt;ac:parameter ac:name="serverId"&gt;c8001771-0b52-30e0-9703-562a2820710f&lt;/ac:parameter&gt;&lt;/ac:structured-macro&gt;&lt;/p&gt;
   ```

Create the macro in confluence with the following options or similar (change as needed):

* Macro Name: jira_issue_filter_vars
* Macro Title: JQL Filter Allowing Variables
* Description: Insert entire JQL query in body of macro. Inject template variables as needed. If quotes needed around variable, insert variable first, then surround variable with quotes.
* Categories: Reporting
* Macro Body Processing: Rendered

How to use JQL Filter Macro in Template with Variables: {#toc-hId-1644675522}
-----------------------------------------------------------------------------

1. Get your query working in JIRA in Advanced mode (text based) with your filter(s) for a single run.
2. Copy advanced JQL text to notepad or clipboard.
   1. Notes for format:
      1. Do not include any line breaks in the JQL
      2. Ensure all whitespaces are spaces and not tabs or other characters
      3. Ensure all special characters such as parenthesis or quotes are basic text (not the special characters sometimes used by MS Word or MS Outlook)
      4. If all else fails, manually re-type the JQL using temporary placeholders for the variables, then using the "$" keyboard command to place the variables into the JQL statement where necessary
3. Create template in Confluence with any header level variables needed. (you can create variables with the gui, but using them later requires you to type $ then the variable name or copying and pasting. If you use the UI again, it will add an additional $ sign to the variable therefore making it a new variable and it won't work.)
4. Add the "JQL Filter Allowing Variables" macro with appropriate options. (it is located in the Reporting category)
   1. Max Rows: only allows a max of 1000
   2. Columns: you may need to use a normal JIRA Issue macro and configure it with your specific columns, then view the source to determine the correct name of some fields. However, the rule so far is that there are no spaces except what is in the field name, and all fields are lowercase, and not surrounded by quotes. Examples of some fields: key, summary,related requirement/s,test case type,test author
5. In the body of the macro, paste in your entire JQL query from step 2  
   ConfMacro1.png
6. Inject your variable or variables into the JQL.  
   ConfMacro2.png
7. If you need to include quotes around variable to support spaces in variable text, add them after injecting the variable(s) into the query.  
   ConfMacro3.png
8. Using this method you can inject multiple variables if needed.  
   ConfMacro4.png
9. Save your template and use as needed.

Hopefully this helps others accomplish this same task. This ends up being really useful if you need to repeat a bunch of JQL filters for different things on different pages in Confluence and you don't have to store these filters as saved filters in JIRA either.  
[](https://community.atlassian.com/t5/Confluence-questions/Can-I-use-a-variable-with-a-macro-I-am-trying-to-create-a-custom/qaq-p/203566?attachment-id=1087){#link_1192cda07b4b85}  
[](/tnckb94959/attachments/tnckb94959/confluence-questions/34257/1/ConfMacro3.png){#display_1192cda07b4b85_0}  
[](https://community.atlassian.com/t5/Confluence-questions/Can-I-use-a-variable-with-a-macro-I-am-trying-to-create-a-custom/qaq-p/203566?attachment-id=1088){#link_1192cda07b4b85_0}  
[](/tnckb94959/attachments/tnckb94959/confluence-questions/34257/2/ConfMacro2.png){#display_1192cda07b4b85_2}  
[](https://community.atlassian.com/t5/Confluence-questions/Can-I-use-a-variable-with-a-macro-I-am-trying-to-create-a-custom/qaq-p/203566?attachment-id=1089){#link_1192cda07b4b85_1}  
[](/tnckb94959/attachments/tnckb94959/confluence-questions/34257/3/ConfMacro4.png){#display_1192cda07b4b85_4}  
[](https://community.atlassian.com/t5/Confluence-questions/Can-I-use-a-variable-with-a-macro-I-am-trying-to-create-a-custom/qaq-p/203566?attachment-id=1090){#link_1192cda07b4b85_2}  
[](/tnckb94959/attachments/tnckb94959/confluence-questions/34257/4/ConfMacro1.png){#display_1192cda07b4b85_6}

comments: author: Max Cascone
body: Confluence Source Editor isn't available for Cloud.

What I want is fairly simple, although clearly more complex in practice.

I want to be able to use Jira macros on Confluence pages set up as templates, and pass a variable into the parameter of the Jira macro.

Has anyone solved this for Cloud?


author: Fabian
body: I would be interested in a solution too.


author: Ryan Colandrea
body: Same here as well.


author: C A
body: Thanks for this workaround.

However this is a static replacement of all Variables during the creation of the page from a template by the variable-content (e.g. Text).

So once the page is created, i cannot modify the content dynamically again.

I am wondering, is there a solution to add a variable to a page, which can also be modified during "edit" and is there a way to access it from the User Macro?

There is this "Easy Confluence Variables Plugin", but as far as I can read from the documentation it allows to define variables on the space level and not on the page level during edit of the page.


author: C A
body: Is there a way to achieve similar results to define and access "variables" using the "page properties macro"?

In that case another macro is also able to access per-page defined parameter...


author: Ryan Colandrea
body: I ended up doing a workaround way to fix this. I use jira cloud so any on prem options were not applicable, as well, I am an end user so any plugins were really not options either.

What I ended up doing is pretty much screen scraping, so while in the editor I run some javascript I wrote, and then save the page. I keep the javascript in a bookmark and just click the bookmark when needed and it quickly loops through all the macros that I needed variables in and changes the value. Then saving the page automatically saves the new value to the database.

Here is our javascript (we needed to replace a story number in the macro):

```
javascript&colon; (function () { 
/*variables change below*/
var oldStoryNumber = 'XX-0000';
var newStoryNumber = 'XX-0001'; 
/*dont change below*/
var dataMacPar = 'data-macro-parameters';
var editInMac = 'editor-inline-macro'; 
/*code*/
var documentCollection = document.getElementsByTagName('iframe')[0].contentDocument.getElementsByClassName(editInMac);

function GetStoryAtrributeAndSetStory(item) {
var stringToChange = item.getAttribute(dataMacPar);
var changedString = stringToChange.replace(oldStoryNumber, newStoryNumber);
item.setAttribute(dataMacPar, changedString);
}
for (var i = 0; i < documentCollection.length; i++) {
GetStoryAtrributeAndSetStory(documentCollection[i]);
}
})();
```

Kind of a back end way to get what I wanted done but I figured I would share! Good Luck!


author: Wim de Smit
body: Great job Adam Barylak - helped me a lot!


author: Alejandro Conde Carrillo
body: You could create a user macro which collects the parameters and call an populate the parameters withing a JIRA macro.

You can add a external macro within your user macro using the following format.

```
&lt;ac:macro ac:name="macroname"&gt;	&lt;ac:parameter ac:name="parameter1"&gt;value1&lt;/ac:parameter&gt;	&lt;ac:parameter ac:name="parameter2"&gt;value2&lt;/ac:parameter&gt;&lt;/ac:macro&gt;
```

Check the storage format of a page (Tools \> Vier Storage Format) containing a JIRA Issues macro similar to what you want to generate to know the format of the JIRA issues macro. You should be able to pass the parameters of your macro to the JIRA issues macro.

comments: author: Max Cascone
body: again... not applicable to Cloud.


Tags: cloud

confluence-cloud

macro

server