How to create custom macro using groovy script?

SKR August 5, 2015

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

0 votes
Bob Swift OSS (Bob Swift Atlassian Apps)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 7, 2015

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

#set ($runMacro = ...)
$action.getHelper().renderConfluenceMacro($runMacro)
SKR August 7, 2015

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

Bob Swift OSS (Bob Swift Atlassian Apps)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 7, 2015

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.

SKR August 7, 2015

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

Bob Swift OSS (Bob Swift Atlassian Apps)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 7, 2015

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?

SKR August 7, 2015

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

SKR August 7, 2015

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

Bob Swift OSS (Bob Swift Atlassian Apps)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 7, 2015

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

SKR August 7, 2015

still no luck :(

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events