Check a field for a value

Check whether a field contains a value, is empty, or is null and act accordingly.

For this example, the data source includes the field orderline.order.contact, and the field is a STRING. The field either contains a value, is an empty string, or is null.

To test whether it contains a value, you write an expression:
orderline.order.contact.trim().length()>0
Tip:

It is good practice to use the trim() function to remove extra whitespace from a string expression.

This expression will evaluate to either TRUE or FALSE.
To explicitly test for an empty string, there are two options:
orderline.order.contact.trim().length()==0
orderline.order.contact.isEmpty()==TRUE
To explicitly test for null:
orderline.order.contact.isNull()==TRUE

Use in the Visibility Condition property

Expressions that evaluate to TRUE or FALSE can be used in the Visibility Condition (visibilityCondition) property. If the expression evaluates as TRUE, then the instance of the element appears in the report. If the expression evaluates as FALSE, the instance of the element (to include all its children, i.e. the entire element tree) is removed from the report. If you are using relative positioning, all sibling elements after this element in the report structure shift accordingly, reclaiming the space that the element would have occupied.

Use in the Text property

You can use these expressions when defining the Text (text) property. The Text property specifies the text to be drawn.
orderline.orders.contact.trim().length()>0?orderline.orders.contact:""
In this expression, the expression evaluates to TRUE when the length of the trimmed field is greater than zero and the field value is printed (to include any leading or trailing spaces). If the length is not greater than zero, the field is identified as not having a value and an empty string is printed; the vertical allocated space for that field remains in the report.

An alternate expression could simply be:

orderline.orders.contact.trim()
Tip:

When you use a character type with a fixed length for a field (such as CHAR(N)), you typically need to add .trim() or .trimRight() to remove trailing spaces. You can avoid this by using the STRING data type. With the STRING data type, the value is not padded with trailing spaces unless trailing spaces are explicitly set.

DEFINE field1 CHAR(5),
DEFINE field2, field3 STRING
LET field1="ABC"   -- you end up with "ABC  "
LET field2="ABC"   -- you end up with "ABC"
LET field3="ABC  " -- you end up with "ABC  ", as explicitly specified