Creating a Lookup or Conversion Table with Expression Language

Modified on Wed, Dec 6, 2023 at 3:18 PM


Conversion Tables


A conversion table is used to convert one set of values to another.   This can be done in Expression Language, with a sort of "run-on" sentence or list of conversion mappings with no space between them.   You string together a set of conditional Expressions each of which converts one specific value into another value (or anything else)


For example, here the program Type Code (of type String) is being converted into a BigDecimal - the exact BigDecimal will depend on what the program type is set to:


${row.programTypeCode  =='22'? rw:toBigDecimal(18.5) : ''}${row.programTypeCode  =='23'? rw:toBigDecimal(19.25) : ''}${row.programTypeCode  =='25'? rw:toBigDecimal(17.45) : ''}${row.programTypeCode  =='30'? rw:toBigDecimal(20.15) : ''}${row.programTypeCode  =='31? rw:toBigDecimal(22.15) : ''}${row.programTypeCode  =='32'? rw:toBigDecimal(22.15) : ''}


The above is a sequence of similar expressions, all of which convert one value to another, if the first value is found.  It the first value is not found, each statement returns a null string.  A 'null string;, otherwise known as an 'empty Strng', is written as two single quotes with nothing - no spaces or characters of any kind - between them:  ''  <-  This is not a double quote.  It is a single quote with nothing between the quotes.  


Here is one of the Expressions:

${row.programTypeCode =='22'? rw:toBigDecimal(18.5) : ''} }


Let's take it apart.   First, notice that it is a conditional statement, a statement of the form A ? B : C  meaning "If A is true, do B otherwise, do C".  See the article on: Conditional Expressions - Decisions in Expression Language.    The "A" part here is row.programTypeCode == '22'

  • row.programTypeCode  is a column of the report.  It has type "String" so it must be compared to a String or tested to see if it is empty.
  • Here, row.programTypeCode  is tested against the value '22'.   '22' is a String, not a number, because the 22 is surrounded by single quotes.  So, row.programTypeCode  == '22' can either be true or false:  It is true when row.programTypeCode  contains the String '22' and it is false all of the rest of the time.
  • ? -> This indicates that this is a Conditional Expression.  If  row.programTypeCode  == '22', the "true" side of the expression is done.  The true side is rw:toBigDecimal(18.5).   If row.programTypeCode  does not equal '22', the "false" side of the conditional expression is done.  The false side is just two single quotes next to each other:  ''  with no spaces inbetween.
  • rw:toBigDecimal(18.5) returns a decimal number of type BigDecimal - making the type of the entire expression "BigDecimal".
  • '' on the otherhand, is an empty String, meaning that the type of the overall expression is not set because there is really nothing there.  And this nothing is very important.


When multiple expressions of this type following each other, as in the example above:  ${row.programTypeCode  =='22'? rw:toBigDecimal(18.5) : ''}${row.programTypeCode  =='23'? rw:toBigDecimal(19.25) : ''}${row.programTypeCode  =='25'? rw:toBigDecimal(17.45) : ''}${row.programTypeCode  =='30'? rw:toBigDecimal(20.15) : ''}${row.programTypeCode  =='31? rw:toBigDecimal(22.15) : ''}${row.programTypeCode  =='32'? rw:toBigDecimal(22.15) : ''}, all of the Expressions will return the null String except - possibly - for one that matches and the test comes up true.   If there is a match, the resulting BigDecimal will be returned as long as there are no spurious characters (e.g. spaces, newlines) between the end of one expression and the beginning of the next.  If nothing matches, the result will also just be "empty". 


Messing Up:  Hidden Spaces or Newlines

What happens if there is a spurious (a space, a newline or anything else) character between the end of one expression and the beginning of the next?   Then the resulting dataType will be "String" and not "BigDecimal"    


Other Examples

Converting Strings to numbers (decimals or integers) is just one of the things you can look for with this kind of of lookup sche

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article