Run and SQL macro - db duplicate check and render message

Ed Gaile
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.
December 5, 2014

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
Answer accepted
Ed Gaile
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.
December 5, 2014

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.

0 votes
Ed Gaile
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.
December 8, 2014

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
TAGS
AUG Leaders

Atlassian Community Events