{"id":1632,"date":"2010-09-13T23:08:23","date_gmt":"2010-09-13T17:38:23","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1632"},"modified":"2010-09-13T23:08:23","modified_gmt":"2010-09-13T17:38:23","slug":"passing-optional-parameters-in-jasper-reports","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/passing-optional-parameters-in-jasper-reports\/","title":{"rendered":"Passing Optional Parameters in jasper reports"},"content":{"rendered":"<p>Hi Guys,<\/p>\n<p>In one of my projects, While working with jasper reports, one of the requirement I faced was of passing optional parameter to the sql query in jasper\u00a0 report. For eg. I had to write a query to find all new customers which were added between two given dates and if an optional status value of the customer is passed the report should should be further refined to include only those customer which were added and had the same status value as passed, If the status is not provided it should be ignored in SQL query in jasper report. So, in this case the status was an optional parameter.<\/p>\n<p>The solution I found was pretty elegant. On searching over the net, I found that parameters with an ! (exclamation) mark are compiled before the query is executed. That is,<\/p>\n<pre lang=\"groovy\">\r\n$P!{status}\r\n<\/pre>\n<p>will be compiled before the query is executed. So, I passed the status from my code in to the jasper report. Now, to take care of the condition that it could be optional, I added a new parameter inside the jasper report, say &#8220;status_value&#8221;. such that, status_value had a default value expression (DFE) :<\/p>\n<pre lang=\"groovy\">\r\n\r\n($P{status}==\"\" ? \" and customer.status=' \"+ $P{status} + \" ' \" )\r\n<\/pre>\n<p>Now in the SQL query in jasper I wrote <\/p>\n<pre lang=\"SQL\" >\r\nselect * from customer where customer.start_date=$P{startDate} and customer.end_date=$P{endDate} \r\n$P!{status_value}\r\n<\/pre>\n<p>what it does is that status_value is compiled before the execution of query. So the  condition in the DFE of status_value checks whether we a receive a status from outside or not, If we do the condition will be appended in the query. Other wise it won&#8217;t be appended and we are done. :).<\/p>\n<p>With Regards<\/p>\n<p>Sachin Anand<br \/>\nsachin@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi Guys, In one of my projects, While working with jasper reports, one of the requirement I faced was of passing optional parameter to the sql query in jasper\u00a0 report. For eg. I had to write a query to find all new customers which were added between two given dates and if an optional status [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":54},"categories":[1],"tags":[159,160,405],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1632"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1632"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1632\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}