Script error thrown from method 'substring'?

Anonymous
Not applicable

Script error thrown from method 'substring'?

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.

4 REPLIES 4
Kenny_Elkington
Marketo Employee

Re: Script error thrown from method 'substring'?

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.

Anonymous
Not applicable

Re: Script error thrown from method 'substring'?

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

Anonymous
Not applicable

Re: Script error thrown from method 'substring'?

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

Anonymous
Not applicable

Re: Script error thrown from method 'substring'?

Hi Jonathan,

I ended up doing something pretty close to that:

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

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

  Hello,

#else

  $display.capitalize($fname),

#end