I've been trying to figure out why my token containing velocity script is prompting an error message in my email when attemptimg to "Approve and Close". The script within the token is supposed to take the first Name token reference and Capitalize the name using the following script:
#set($firstName = $lead.FirstName)
#set($formattedFirstName = $firstName.toLowerCase().substring(0,1).toUpperCase() + $firstName.toLowerCase().substring(1))
${formattedFirstName}
No matter how I try to implement the script, wheather directly into the email (breaking it from the template) or applying the script to a custom token. Does anyone have any input into why this is happening?
Solved! Go to Solution.
Simple. It’s because the $lead.FirstName
property is an empty String when you’re approving the asset. There’s no lead context at that point. And String offsets > 0 on an empty string throw fatal errors.
So you need to check if it’s empty first (a good idea in any case).
#set( $firstName = $lead.FirstName )
#if( !$firstName.isEmpty() )
#set( $formattedFirstName = $firstName.toLowerCase().substring(0,1).toUpperCase() + $firstName.toLowerCase().substring(1))
${formattedFirstName}
#end
Though the code is somewhat redundant and can just be:
#set( $firstName = $lead.FirstName )
#if( !$firstName.isEmpty() )
#set( $formattedFirstName = $firstName.substring(0,1).toUpperCase() + $firstName.substring(1).toLowerCase())
${formattedFirstName}
#end
And in fact you can sidestep the empty check completely if you use the DisplayTool helper, which will never error out on an empty String:
#set($firstName = $lead.FirstName)
#set($formattedFirstName = $display.capitalize($firstName.toLowerCase()))
${formattedFirstName}
Simple. It’s because the $lead.FirstName
property is an empty String when you’re approving the asset. There’s no lead context at that point. And String offsets > 0 on an empty string throw fatal errors.
So you need to check if it’s empty first (a good idea in any case).
#set( $firstName = $lead.FirstName )
#if( !$firstName.isEmpty() )
#set( $formattedFirstName = $firstName.toLowerCase().substring(0,1).toUpperCase() + $firstName.toLowerCase().substring(1))
${formattedFirstName}
#end
Though the code is somewhat redundant and can just be:
#set( $firstName = $lead.FirstName )
#if( !$firstName.isEmpty() )
#set( $formattedFirstName = $firstName.substring(0,1).toUpperCase() + $firstName.substring(1).toLowerCase())
${formattedFirstName}
#end
And in fact you can sidestep the empty check completely if you use the DisplayTool helper, which will never error out on an empty String:
#set($firstName = $lead.FirstName)
#set($formattedFirstName = $display.capitalize($firstName.toLowerCase()))
${formattedFirstName}
There's a "$" missing in line #2. It needs to be:
#if( !$firstName.isEmpty() )
There's a "$" missing in line #2. It needs to be:
Good call, fixed.