Need Help - My Velocity Scripted Token Not Allowed | Community
Skip to main content
Level 2
July 7, 2025
Solved

Need Help - My Velocity Scripted Token Not Allowed

  • July 7, 2025
  • 1 reply
  • 747 views

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?

Best answer by SanfordWhiteman

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}

 

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
July 8, 2025

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}

 

Michael_Florin-2
Level 10
July 8, 2025

There's a "$" missing in line #2. It needs to be:

#if( !$firstName.isEmpty() )

 

SanfordWhiteman
Level 10
July 8, 2025

There's a "$" missing in line #2. It needs to be:

Good call, fixed.