Script error thrown from method 'substring'? | Community
Skip to main content
May 6, 2016
Question

Script error thrown from method 'substring'?

  • May 6, 2016
  • 2 replies
  • 1758 views

I'm trying to create a quick token to capitalize a lead's first name. I referenced this conversation: https://nation.marketo.com/message/83535#comment-83535

The code is:

#set($name = ${lead.FirstName})

$name.substring(0,1).toUpperCase()$name.substring(1).toLowerCase()

And I checked the box next to First Name.

it's dropped into an email as: {{my.FName Capitalized:default=Hello}},

At first blush, it seemed to work. But now I'm getting an error whenever I try to save the email I've placed it on:

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

?

This might be a little beyond my skill level. Any suggestions? Or maybe another way to do this to avoid the error? I know we want some backend stuff that checks names, but that's for later. This is our quick fix for the time being.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

Kenny_Elkington
Adobe Employee
Adobe Employee
May 6, 2016

This will happen if the first name is empty or is only one character long.  You'll need to add a check to see if it's either null or only has a length of 1, and then output and output an alternative value.

May 6, 2016

Ok, I've gotten a little further, but still no success. I'm sure it's just me not knowing Velocity.

I put it in a basic if statement for null or empty. No more errors, but it's skipping over the person's name and giving me the default "hello," no matter what:

#if($Lead.FirstName && !$Lead.FirstName.empty)

    $name.substring(0,1).toUpperCase()$name.substring(1).toLowerCase()

#else

    Hello,

#end

June 1, 2016

Hi Charles, not sure if you got any further with this. I'm also a novice velocity scripter but seemed to get this to work by using the following:

#if(${lead.FirstName} == "")

colleague

#else

#set($name = ${lead.FirstName})

$name.substring(0,1).toUpperCase()$name.substring(1).toLowerCase()

#end

June 1, 2016

Hi Jonathan,

I ended up doing something pretty close to that:

#set ($fname = ${lead.FirstName})

#if($fname.equals(""))

  Hello,

#else

  $display.capitalize($fname),

#end