Jump to content
Changes to the Jaspersoft community edition download ×

variable and subreport


Recommended Posts


I've got a report with a subreport in the section "detail".

My report shows the country and the subreport shows the regions by country.

I'd like to show in the lastpage footer of the report (country), the total count of regions.

I try with variable and scriptlet but it doesn't work.

Link to comment
Share on other sites

  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

The inbuilt REPORT_COUNT variable of the subreport will return the number of rows (regions) for each country. In this example we'll call it myRegionCount.



Remember to set the Calculation type to "System" for the local variable (in the main report) that you will pass the REPORT_COUNT value to.



Now you need a function that will let you accumulate the returned values from each row. Add a function like this one to your scriptlet:



public Double addVariables(Integer v1, Integer v2) throws JRScriptletException


Integer p = (java.lang.Integer)(v1.intValue() + v2.intValue());

return p;




Now you'll need another variable to call the routine and store the accumulated values. For this example we'll call it myAccumulator.



Set the Calculation Type to System, the Initial Value to new java.lang.Integer(0), and the Variable Expression to $P{REPORT_SCRIPTLET}.addVariables($V{myAccumulator}, $V{myRegionCount})



And finally you'll need to 'display' the variable on each row to call the function. Define a new Text Field with the Textfield Expression set to $V{myAccumulator}. Of course you can leave it hidden if you don't need it to be visible on each row, but it's useful to display it while you're getting things going.



Once all of the main report's rows have been filled then the myAccumulator variable should contain the total number of regions. Have fun with it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...