SOLVED

Re: Velocity working in preview mode, not in sample mail

Go to solution
Franky_Ruyssch2
Level 4

Velocity working in preview mode, not in sample mail

Hi I have the following velocity script that takes, the post code from the lead. If the length of the postalcode is 4 it is converted to a number for further use, if the length is different from 4 ( in which all cases it is 6 ) , i have to take the first 4 characters, and convert them to a number.

#set( $start = 0 )
#set( $end = 4 )
#set( $postalCode = ${lead.PostalCode} )
#if ( $postalCode.length().equals(4) )
#set( $postalInt = $convert.toNumber("0${postalCode}"))
#else
#set( $val = $lead.PostalCode.substring($start,$end))
##set( $postalInt = $convert.toNumber("0${val}"))
#end

${val}

The code is working in preview mode ( View By : Person ), but when i send a test mail I receive : 

An error occurred when procesing the email Rendered_Email_Velocity_Error_Area_?!

Invocation of method 'substring' in class java.lang.String threw exception java.lang.StringIndexOutOfBoundsException: String index out of range: 4 near

?

What is wrong here???

Franky Ruysschaert
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity working in preview mode, not in sample mail

When posting code, please highlight it using the Syntax Highlighter.

The behavior with samples is expected.  A sample email does not have the full lead and CO context, so only use real emails and Preview-by-List to test Velocity.

However, the error message also shows the need for more defensive programming. Truth is you don't know that $lead.PostalCode will always have a non-empty value.  Seeking index 4 of an empty String is always a fatal error in Java, and Velocity can't swallow that up. So you need to check for emptiness.

And you should take the leftmost N characters up to a maximum of 4. There's no reason to process a 6-char string and a 4-char string differently.

Don't know what you're trying to do with "0{$reference}" but that isn't necessary to cast a numeric String to a Number. And do you really want a Number? I doubt it, as these values can fit in an Integer.

#set( $postalCode = $lead.PostalCode )
#set( $maxLength = 4 )
#set( $length = $math.min($maxLength,$postalCode.length()) )
#if( !$postalCode.isEmpty() )
#set( $postalInt = $convert.toInteger($postalCode.substring(0,$length) ) )
#end‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

1 REPLY 1
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity working in preview mode, not in sample mail

When posting code, please highlight it using the Syntax Highlighter.

The behavior with samples is expected.  A sample email does not have the full lead and CO context, so only use real emails and Preview-by-List to test Velocity.

However, the error message also shows the need for more defensive programming. Truth is you don't know that $lead.PostalCode will always have a non-empty value.  Seeking index 4 of an empty String is always a fatal error in Java, and Velocity can't swallow that up. So you need to check for emptiness.

And you should take the leftmost N characters up to a maximum of 4. There's no reason to process a 6-char string and a 4-char string differently.

Don't know what you're trying to do with "0{$reference}" but that isn't necessary to cast a numeric String to a Number. And do you really want a Number? I doubt it, as these values can fit in an Integer.

#set( $postalCode = $lead.PostalCode )
#set( $maxLength = 4 )
#set( $length = $math.min($maxLength,$postalCode.length()) )
#if( !$postalCode.isEmpty() )
#set( $postalInt = $convert.toInteger($postalCode.substring(0,$length) ) )
#end‍‍‍‍‍‍‍‍‍‍‍‍