Run and SQL macro - db duplicate check and render message
Asked by Ed Gaile:

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.


Answers:
author: Ed Gaile
body: Bob answered my question on his site:  
* ![](https://bobswift.atlassian.net/wiki/download/attachments/10584065/bswift-39779-pp-bob.jpg?version=1&modificationDate=1340464169852&api=v2)[Bob Swift](https://bobswift.atlassian.net/wiki/questions/users?username=bswift)  
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](http://stackoverflow.com/questions/17045923/insert-or-update-in-function-unique-key-violation-transaction-issue).


author: Ed Gaile
body: 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.RenderModedef 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}


Tags: run