velocity script newbie - lowercase first initial

JD_Nelson
Level 10 - Community Advisor

Hi there - just learned that velocity scripting exists in Marketo! Where have I been, under a rock somewhere!? Anyway, now I just wish I knew how to use it. In the meantime, maybe someone can help point me in the right direction on how to get a lowercase first initial for my lead records?  I tried to modify an uppercase script (granted it does the whole name, but 1 thing at a time) and I couldn't even get that to work (changed 'capitalize' to 'lowercase').

Thanks all!!

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

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

there

#else

$display.lowercase($fname)

#end

21 REPLIES 21
Nicholas_Manojl
Level 9

Ah right.

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

$x.charAt(0)

Mark me correct, I want the points

JD_Nelson
Level 10 - Community Advisor

Sorry, now I'm not sure how to combine both of these; lowercase and first character only.

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

$display.uncapitalize($fname)

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

$x.charAt(0)

(did not work, obviously)

Nicholas_Manojl
Level 9

It's as simple as..

#set ($x = ${lead.FirstName.toLowerCase()})

$x.charAt(0)

JD_Nelson
Level 10 - Community Advisor

An error occurred when procesing the email Rendered_Email_Velocity_Error_Area_?!

Invocation of method 'charAt' in class java.lang.String threw exception java.lang.StringIndexOutOfBoundsException: String index out of range: 0 near

Screenshot 2016-07-27 07.46.05.png

and then I call the script in my email using {{my.firstinitial}}

What am I doing wrong?

(Sorry for the back and forth, I don't really have a grasp on this language yet)

Nicholas_Manojl
Level 9

hmm. I think it's a bug Kenny Elkington


It works in the email preview - according to Email Scripting » Marketo Developers  anything that works in the email preview is supposed to work in an actual email, but in this case it doesn't.

Sorry.

SanfordWhiteman
Level 10 - Community Moderator

pastedImage_0.png

Nicholas_Manojl
Level 9

Did you try that in the preview or in the actual email send?

It still errors at my end.

SanfordWhiteman
Level 10 - Community Moderator

Actual, non-sample send.

Nicholas_Manojl
Level 9

Weird.. your syntax doesn't look right.

Shouldn't it contain the braces?

#set ($fi = ${lead.FirstName.toLowerCase().charAt(0)})

$fi


Even with your syntax, I'm still finding the email won't approve.

SanfordWhiteman
Level 10 - Community Moderator

You don't need formal syntax in a #set directive unless there some syntactical confusion (like a reference right next to a constant string).

What's the error you're getting?

Nicholas_Manojl
Level 9

Well, I was able to get it to send in an actual campaign send.

But the email won't approve and the error is:

Validation Error approving testttt.kefe

An error occurred when procesing the email Rendered_Email_Velocity_Error_Area_?!

Invocation of method 'charAt' in class java.lang.String threw exception java.lang.StringIndexOutOfBoundsException: String index out of range: 0 near

But as I'd had the email approved with my script token already included, I was able to send the test.

SanfordWhiteman
Level 10 - Community Moderator

You should check the length of the string before trying to get an offset (index).

Nicholas_Manojl
Level 9

I tried that yesterday.. I think it went something like

$i = firstname.length - firstname.length

charAt(i)

But are you sure that's the issue - as above, I can send in an actual campaign, and the preview works. It is just the email approval and sample send that fails.

SanfordWhiteman
Level 10 - Community Moderator

Yes, I'm quite sure that's the issue. That exception is thrown if you have a string of length 0 (which does not have a charAt(0)).

#if(!$lead.FirstName.isEmpty())

    #set($fi = $lead.FirstName.toLowerCase().charAt(0))

#end

Firstly, $!{fi}...

Frank_Breen2
Level 10

Also it seems like you didn't have First Name selected in the Lead Section of the Script Token, this is why you see that output:

vol-script.png

Nicholas_Manojl
Level 9

It's actually like this:

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

<p>Hi $fname</p>

Just be mindful this actually makes the entire word lowercase.

Alternatively you could make just the first letter lower case:

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

<p>Hi $display.uncapitalize($fname)</p>

Or you could make everything uppercase first, and then just make the first letter lowercase (why?).

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

<p>Hi $display.uncapitalize($fname)</p>

JD_Nelson
Level 10 - Community Advisor

what if I just want the first letter of the first name?

i.e. "j"

Nicholas_Manojl
Level 9

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

<p>Hi $display.uncapitalize($fname)</p>

So it someone is in your database as FRANK, it will render as fRANK.

JD_Nelson
Level 10 - Community Advisor

but I just need it to render as "f"

I saw something about a substring to pull just the first character, but not sure how to code that in this instance.

Frank_Breen2
Level 10

JD I think this is the code you want:

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

$display.uncapitalize($fname)

Info here. Look at this article too.