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"/>
check your variable calculation type is "sum" or "None". if your calculation type is "None", variable behavior seems normal because I assume that you sum with a constant value. Set the calculation type "Sum" and Reser type "report", Go the textbox on the report that holds your variable. Set the avaluation time "Now".
Parameters promptDrag the parameter from the report inspector inside the title band. iReport creates a textfield to display the parameter value. Run the report using an empty data source by clicking the preview button. The parameter prompt dialog will appear asking for a value for the MESSAGE parameter.
To add fields to your report, click the field in the Outline view and drag it to the Design view. When the field object is dragged inside the detail band, Jaspersoft Studio creates a text field element and sets the text field expression for that element.
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"/>
Im not exactly shure how to write it in JRXML since i use iReport. In iReport, i create a new Variable, with class type "Integer", and calculation type "System" The calculation type is important here.
In the variable expression, you will need something like $V{grandCount} = $V{grandCount} + $V{itemCount}
NOTE: JasperReports render band by band, so you wont be able to use the grandCount variable in a band before the subreport band.
Hope im not too late
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With