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

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:

 

 

{groovy|output=wiki}

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}

{sql:datasource=custom_app_db_dev|table=false}

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

{sql}

{run}

"""

 out.println runMacro

{groovy}

 

 

 

 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Asked Thursday in Confluence

What are the resources that you use to learn more about Atlassian Products?

I am gathering information about resources available for Atlassian product knowledge transferring for a presentation in our local Atlassian User Group. I want to group them in four categories From ...

159 views 5 7
View question

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