JasperReports Oddities #1: “Print When” Expressions

The peculiarities of Print When and Hyperlink When expressions in JasperReports probably shouldn’t – and possibly couldn’t – be underestimated, particularly when it revolves around $F{…} fields returning text rather than numeric or null values.

E.g. This is easy:

$F{myField} != null

So is this:

$F{myField} > 0

Or this:

AND($F{myField} != null, $P{myParameter} > 0)

These are all simple, indeed obvious, constructions, and work as expected. But what happens when a text value is returned, and you only want it to print when your returned value is not a specific result? Maybe your SQL looks like this:

CASE WHEN t.myvalue IS NULL THEN 'No Value'

Now using != just won’t work for text – an idiosyncrasy of Jaspersoft – so instead we need something more like this:

$F{myField}.equals("No Value")

Oops, well mostly. But we want it to print when it’s not equal to “No value”, so instead let’s have this:

!$F{Site name}.equals("No Value")

Great! So now we have it checking for the returned value and only printing or creating a hyperlink when the value is not the adjusted null (who’s idea was this, anyway?). But let’s have one final tweak– it’s not critical, but instead for the sake of neatness – and also so we can dispose of any requirement to worry about the result case:

!$F{myField}.equalsIgnoreCase("no value")

And that’s it. Our final, working Print When or Hyperlink When expression. Now, when thrown into a sample piece of jrxml, it may look a lot like this:

<textField isStretchWithOverflow="true" isBlankWhenNull="true" hyperlinkType="Reference" hyperlinkTarget="Blank">
<reportElement style="alternateRowsSiteHyperlink" positionType="Float" stretchType="RelativeToTallestObject" x="1400" y="0" width="100" height="30" uuid="5bde21cd-2e78-42b2-bff7-4992bddfade3"/>

<textElement textAlignment="Left">
<font size="9" isUnderline="false"/>


<![CDATA["http://philipobrien.net/" + $F{myField}]]>

<![CDATA[!$F{myField}.equalsIgnoreCase("no value")]]>