How to create custom macro using groovy script?

Hi,

Does anyone knows how to create a custom/user macro using RUN plugin & groovy script, so that I can call macro on each page.

Here is the code that I'm trying to create Macro

{run:replace=a1::hidden value:Hidden description:hidden, g1:$current_user_id::hidden, g2:$page_title::hidden, g3:$page_id::hidden, r1::I have read this page:radio::1:Yes} 
 
{groovy:output=wiki} 
 
import com.atlassian.renderer.v2.RenderMode 
def renderMode = RenderMode.suppress(RenderMode.F_FIRST_PARA) 
def choiceSql = "SELECT distinct(page_id) FROM test_Wiki_Read_Status WHERE page_id ='$g3' AND ad_user = '$g1'" 
def choiceMacro = "{sql-query:datasource=testDS|table=false} ${choiceSql} {sql-query}" def choices = subRenderer.render(choiceMacro, context, renderMode) 
 
if(choices !=''){ 
def runMacro1 = """ 
{sql-query:datasource=testDS} 
UPDATE test_Wiki_Read_Status SET read_status = '$r1' WHERE page_id = '$g3' AND ad_user = '$g1' 
{sql-query} """ 
out.println(runMacro1) 
}else{ 
def runMacro = """ 
{sql-query:datasource=testDS} INSERT INTO test_Wiki_Read_Status (ad_user, page_id, page_title, read_status) VALUES('$g1','$g3','$g2','$r1')
{sql-query} """ 
out.println(runMacro) 
} 
{groovy} 
{run}

1 answer

I believe you need to create a user macro in the normal way and then use render support:

#set ($runMacro = ...)
$action.getHelper().renderConfluenceMacro($runMacro)

Thanks for your response. Could you please explain in detailed?

That is the detail for the user macro implementation. You just need your specific run macro text string from your question copied into the variable. You are rendering that macro in the user macro.

I have tried as you said but I'm getting below error message: "Error rendering macro 'pagereadstatus' : Error occurred rendering template content" Here is the code that I'm using #set($runMacro2 = '{run:replace=a1::hidden value:Hidden description:hidden, g1:$current_user_id::hidden, g2:$page_title::hidden, g3:$page_id::hidden, r1::I have read this page:radio::1:Yes} {groovy:output=wiki} import com.atlassian.renderer.v2.RenderMode def renderMode = RenderMode.suppress(RenderMode.F_FIRST_PARA) def choiceSql = "SELECT distinct(page_id) FROM AC_Wiki_Read_Status WHERE page_id =\'$g3\' AND ad_user = \'$g1\'" def choiceMacro = "{sql-query:datasource=testDS|table=false} ${choiceSql} {sql-query}" def choices = subRenderer.render(choiceMacro, context, renderMode) if(choices !=''){ def runMacro1 = """ {sql-query:datasource=testDS} UPDATE AC_Wiki_Read_Status SET read_status = \'$r1\' WHERE page_id = \'$g3\' AND ad_user = \'$g1\' {sql-query} """ out.println(runMacro1) }else{ def runMacro = """ {sql-query:datasource=testDS} INSERT INTO AC_Wiki_Read_Status (ad_user, page_id, page_title, read_status) VALUES(\'$g1\',\'$g3\',\'$g2\',\'$r1\') {sql-query} """ out.println(runMacro) } {groovy} {run}') $action.getHelper().renderConfluenceMacro($runMacro2) Please help me with this

I am sorry, debugging your user macro is outside our scope for support. Have you considered using a simpler approach using a page include instead of a user macro?

I'm very sorry to bother you, yes I have tried and It's working absolutely fine.

Can we use RUN, Groovy and SQL plugin inside user macro and would that work fine?

Yes, there normally isn't a problem with most macros.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

77 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