Advanced expression to handle NULL values

I want to create an expression that concatenates 2 fields, but in some cases, one of the fields may be null.

When one of the fields is null, I want a blank value used instead.


OrgLevel1.ReportingCategoryDescription + ’ - ’ + OrgLevel2.ReportingCategoryDescription

If any of these fields return a null or blank, I only want to see the field that does have a value.

When the expression engine (which uses .Net C# syntax) evaluates your above expression and one of the values is NULL, it will result in a NULL.

If you wish to replace part of your expression with a blank when the value is NULL, you will need to check for a NULL, and if the value is NULL, replace it with an empty string.

Here is an example of string concatenation and checking for nulls:-

String.Concat(OrgLevel1.Code, (!String.IsNullOrWhiteSpace(OrgLevel2.Code) == true)? '-' + OrgLevel2.Code:'')

The important piece is:-

(!String.IsNullOrWhiteSpace(OrgLevel2.Code) == true)? '-' + OrgLevel2.Code:''

It checks if OrgLevel2.Code is Null or WhiteSpace, if it is NOT, then it uses the value from OrgLevel2.Code, if it is NULL, then it uses an empty string

1 Like