Run and SQL macro - db duplicate check and render message

We are using Bob Swift's Run and SQL macros and trying to do something that seems would be pretty straight forward, however we are hitting a snag.  The functionality we are trying is to use a run macro with form to obtain user input parameters.  The parameters are passed into the SQL query to perform a select (dup check). 

I would like to evaluate the sql reply inside the run macro and if we get a hit present a 'object exists' message, else if we do not get a hit - insert into the db with the user parameters and present a 'object inserted' message.

I have tried putting a groovy macro inside the run macro to drive the if/then but the run macro does not seem to like that.  Has anyone done something similar?  I know this has to be able to be done I am just missing something in front of my nose.

2 answers

1 accepted

0 votes
Accepted answer

Bob answered my question on his site:

I think the best thing to do is to keep the logic isolated and simple. Create an SQL function in your database that does an update or insert. Similar to the answer to this question.

For anyone wanting to do something similar, you can define SQL functions in the database and then call them from the run macro using the sql macro - see example below:




import com.atlassian.renderer.v2.RenderMode
def renderMode = RenderMode.suppress(RenderMode.F_FIRST_PARA)

// define domain list from database def domainSql = "select distinct(DOMAIN_NAME) , ':' , DOMAIN_NAME , ':' as domain from tbl_domains order by DOMAIN_NAME asc" def domainMacro = "{cache}{sql-query:datasource=custom_app_db_dev|table=false} ${domainSql} {sql-query}{cache}" def domains = subRenderer.render(domainMacro, context, renderMode)

//define systemarea list from database def systemareaSql = "select distinct(SSA_NAME) , ':' , SSA_NAME , ':' as systemarea from tbl_service_system_area order by SSA_NAME asc" def systemareaMacro = "{cache}{sql-query:datasource=custom_app_db_dev|table=false} ${systemareaSql} {sql-query}{cache}" def systemareas = subRenderer.render(systemareaMacro, context, renderMode)


//define runMacro variable to display the form

def runMacro = """

{run:id=addservice|autorun=false|requestActions = printresults|Heading=Service Information Form|titleRun=Add New Service|dateformat=yyyy-MM-dd|inputSize=75|replace=g1::?Name:group, serv::?Service name only. DO NOT add version information:text-required-end, g2::?Service Information:group, type::Type:select::BS:BS:FGS:FGS, dom::Domain:select::${domains}, ssa::Service System Area:select-end::${systemareas},desc::Description:textarea|keepRequestParameters = false}


select SERVICEDUPCHECK('\$serv','\$type','\$dom','\$ssa','\$desc')




 out.println runMacro






Suggest an answer

Log in or Sign up to answer
Community showcase
Published Dec 18, 2018 in Confluence Cloud

Happy holidays from our team to yours!

Hi Community!  2018 was filled with changes for our team, both big and small, and we've taken a lot of time to both celebrate our wins and recognize areas of improvement. One thing that we're a...

473 views 3 18
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