How can I increment a variable with value of another variable in JasperReports?

sventechie picture sventechie · Sep 29, 2009 · Viewed 41.6k times · Source

I need to make a grand total of the items I'm counting in a subReport. To do that, I think I need to add the value of that variable to another variable for each iteration, or "increment" it by that value. The subReport gets called for each group, and I get a total for that group. I need to add the variable values, rather than database columns/fields.

I'm receiving an integer returnValue from the subReport, which is itself the count of rows in the sub-report. I want to get the grand total, since that subReport is called multiple times for the different results (each for a GROUP) from my main SQL query. I want to add up all the results, but I'm getting a null value. I tried adding an operation to the subReport as a new returnValue and choosing Sum as the operation, but that also yielded a null.


   <variable name="itemCount" class="java.lang.Integer" resetType="None"/>
   <variable name="grandCount" 
      class="java.lang.Integer" 
      incrementType="Group" 
      incrementGroup="ITEM_BUNDLE">
      <variableExpression><![CDATA[$V{itemCount}]]></variableExpression>
   </variable>

... <returnValue subreportVariable="countItems" toVariable="itemCount"/>

Answer

cetnar picture cetnar · Nov 17, 2009

Add attribute calculation="Sum" to variable name="grandCount"

or pass grandCount to subreport as parameter

<subreportParameter name="grandCount">
<subreportParameterExpression><![CDATA[$P{grandCount}]]></subreportParameterExpression>
</subreportParameter>

in subreport declare variable countItems with initialValue of parameter grantCount

<variable name="countItems" .... >
   <variableExpression><![CDATA[$P{itemCount} + $P{grandCount}]]></variableExpression>
   <initialValueExpression><![CDATA[$P{grandCount}]]></initialValueExpression>
</variable>

and return

<returnValue subreportVariable="countItems" toVariable="grandCount" calculation="Sum"/>