Problem getting a macro parameter

<xhtml-macro name="clientinfo" key="clientinfo"
                 class="com.atlassian.confluence.plugins.clients.ClientinfoMacro">
        <description>A macro that extracts the client information from a JIRA issue and returns it as a JSONObject.
        </description>
        <category name="admin"/>
        <parameters>
            <parameter name="issuekey" type="string" default="CI-02" required="true"/>
        </parameters>
    </xhtml-macro>

    <xhtml-macro name="printinfo" key="printinfo"
                 class="com.atlassian.confluence.plugins.clients.PrinterMacro">
        <description>A macro that prints out the information from the fields in a JIRA issue.</description>
        <category name="admin"/>
        <parameters>
            <parameter name="add" type="string" default="City"/>
        </parameters>
    </xhtml-macro>

So this is part of my atlassian-plugin.xml
As you can see I have 2 macros here, the 1st one I use to get the information from a JIRA issue and put it in a context, and the 2nd should get information from that context, the idea being not to make 1000 requests to JIRA so as to get the info from each field.

public class ClientinfoMacro implements Macro {

    @Override
    public String execute(Map<String, String> params, String arg1, ConversionContext conversionContext)
            throws MacroExecutionException {

        String key = params.get("issuekey");
        Hashmap<String, String> map = new FieldMap().parse(fields);
        conversionContext.setProperty("fieldMap", map);
        conversionContext.setProperty("issueJson", issue);

This is how I get the parameter in the 1st macro (params.get("issuekey")) and it works. I've also included the properties I pass to the 2nd macro.

public class PrinterMacro implements Macro {

    @Override
    public String execute(Map<String, String> params, String s, ConversionContext conversionContext)
            throws MacroExecutionException {

        String add = params.get("add");
        HashMap<String, String> fieldMap = (HashMap<String,String>)conversionContext.getProperty("fieldMap");
        JSONObject issue = (JSONObject)conversionContext.getProperty("issueJson");

That's how I try to get the parameter in the 2nd macro (params.get("add")), but for some reason it doesn't work. I cast the properties from the conversionContext properly, I've checked, but for some reason the add parameter is null, despite of me typing in it's field.

java.lang.NullPointerException
   at com.atlassian.confluence.plugins.clients.PrinterMacro.execute(PrinterMacro.java:34)

34->  System.out.println("prop id: "+fieldMap.get(add));

Any idea as to why I can't get the parameter in the 2nd macro?


1 answer

1 accepted

1 vote
Accepted answer
import org.apache.commons.lang.StringUtils;

String key = StringUtils.defaultString(params.get("issuekey"), "CI-2");
String add = StringUtils.defaultString(par.get("getfield"), "Client Type");

After some time spent on BitBucket going through other people's code I saw they used StringUtils and when I tried it, it worked. Now both parameters return the correct value.

For anyone wondering why this fixes it, default values defined in the plugin .xml aren't actually passed to the macro if they are selected, so you have to provide the default value server side in code. 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Friday in Agile

Are you a Jira Service Desk agent? We want to talk to you!

Are you a whiz at handling tickets and looking at how you can further optimize your workflow with automation? Do you tackle detailed customer support questions while simultaneously getting flooded wi...

90 views 0 5
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