I solved my problem (Well, Sergio Michels helped me). This is the code I used (I changed some domain class and variables names to offer a general solution):
def createReport = {
Map reportParams = [:]
byte[] bytes
//report parameters
reportParams.put("PARAM_1",params.param1)
reportParams.put("PARAM_2",params.param2)
reportParams.put("PARAM_N",params.paramn)
//here I had to write some IF's to know what report to send
if(aCondition1)
def reportDef = new JasperReportDef(name:'my_report1.jasper',fileFormat:JasperExportFormat.PDF_FORMAT,parameters: reportParams)
bytes = jasperService.generateReport(reportDef).toByteArray()
//update records after they are reported
doSomeUpdate param1
}
if(aCondition2)
def reportDef = new JasperReportDef(name:'my_report2.jasper',fileFormat:JasperExportFormat.PDF_FORMAT,parameters: reportParams)
bytes = jasperService.generateReport(reportDef).toByteArray()
//update records after they are reported
doSomeUpdate param2
}
//send report for download
response.addHeader("Content-Disposition", 'attachment; filename="report.pdf"')
response.contentType = 'application/pdf'
response.outputStream << bytes
response.outputStream.flush()
}
I found that for this case is better to use the server instead of the chain. This works as a charm!