Solved! Go to Solution.
French, German, and Dutch surnames among others may have prefixes like "von", "de", and "van". These may be separable ("van der Steen" is alphabetized under "S") or an inherent part of the name, in which case they are generally capitalized. Getting this wrong may be insulting or confusing.
I keep getting this error message:
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: 1 near
Any thoughts?
Hi Noah,
This is Velocity in a nutshell (and don't get me wrong, I love it, it's just quirky): flexible when you don't need it to be, and strict when you don't expect it!
When Velocity calls deep enough into the outer Java environment, you can get error conditions where you might've thought Velo would let you slide.
A prime example is when you try to take the substring of an existing variable that is empty, with any index other than 0:
#set( $a = "" )
$!{b.substring(0,1)} <-- no error!
$!{a.startsWith("ABCDE")} <-- no error!
$!{a.substring(0,1)} <-- error!
Note how calling substring() on the nonexistent variable $b doesn't cause an error, but the substring on existing-but-empty $a is an error. This is what I mean by the flexible vs. strict dichotomy being kind of surprising.
To avoid this error, wrap in isEmpty():
#if( !$name.isEmpty() )
${name.substring(0,1).toUpperCase()}${name.substring(1).toLowerCase()}
#end
Also see my comments here, and if you're getting into VTL in Marketo you should read and/or subscribe to my blog, if I do say so. See my VTL posts at http://blog.teknkl.com/tag/velocity.
Oh, and also it's almost always better to open a new thread (you can link back to an old one for reference). Old threads can never be closed by anyone but the OP, and more than one answer can never be marked Correct.
Hi @SanfordWhiteman, can you clarify how to wrap in isEmpty() ? How should the final velocity script look combining the two scripts?
Script #1
#set($name = ${lead.FirstName})
$name.substring(0,1).toUpperCase()$name.substring(1).toLowerCase()
Script #2
#if( !$name.isEmpty() )
${name.substring(0,1).toUpperCase()}${name.substring(1).toLowerCase()}
#end
Correct combined scripts:
#set($name = ${lead.FirstName})
#if( !$name.isEmpty() )
${name.substring(0,1).toUpperCase()}${name.substring(1).toLowerCase()}
#end
One little thing. 🤓 You shouldn't use curly braces in directives (#set), only in output. (They happen to work in this simple case, but as code gets more complex they lead to syntax errors.)
#set( $name = $lead.FirstName )
#if( !$name.isEmpty() )
${name.substring(0,1).toUpperCase()}${name.substring(1).toLowerCase()}
#end
Could you highlight your code (as Java) please using the Syntax Highlighter here on the forum?
@SanfordWhiteman, updated.
Hi Sanford,
Success! Thanks so much for the help. Brand new to Velocity but your reply explains things very clearly.
I've subscribed to your blog!
Thanks,
Noah
Great x2!
One more quick question about this script.
This appears to add an extra space after the token. For example, the output is "Dear XXX ,"
How can I remove that space in between the end of the name and the comma?
It's probably a carriage return, not a space -- then coerced to a space by HTML.
${name.substring(0,1).toUpperCase()}${name.substring(1).toLowerCase()}##
Whitespace is often (depending on context) preserved in Velo, which can lead to fun times.