SOLVED

Velocity Script - Clean Up First Name Field

Go to solution
Broderick_Klem1
Level 4

Velocity Script - Clean Up First Name Field

I'm currently use this email script token to clean up our first name field which often contains email addresses, periods, full names, and title case it, etc.

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

  there##

#elseif($lead.FirstName.contains("@")) 

  there##

#elseif($lead.FirstName.contains("."))

  there##

#elseif($lead.FirstName.contains(" "))

  #set($name = $lead.FirstName.split(" ")[0])

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

#else  

  #set($name = $lead.FirstName)

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

#end

The one thing I can't figure out how to do is to identify when a first name field contains less than 2 characters so that on line #07 where we split the name, if the subsequent $name < 2 characters (e.g. "A Johnson" becoming "A") then just use "there". How do I do that?

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script - Clean Up First Name Field

First, note this approach doesn't work for people with hyphenated first names. "Jean-Marie" should not be changed to "Jean-marie," that's not right.  (Similarly, people with a compound first name may not use a hyphen, like "Norma Jean," but that's the first name just the same, not "Norma.")

As for determining if something has N characters:

#if( $name.length() > 2 )

$display.capitalize($lead.FirstName.toLowerCase())##

#else

there##

#end

But there, too, you're saying that "AJ" and "B.B." aren't people's real-life first names.

View solution in original post

5 REPLIES 5
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script - Clean Up First Name Field

First, note this approach doesn't work for people with hyphenated first names. "Jean-Marie" should not be changed to "Jean-marie," that's not right.  (Similarly, people with a compound first name may not use a hyphen, like "Norma Jean," but that's the first name just the same, not "Norma.")

As for determining if something has N characters:

#if( $name.length() > 2 )

$display.capitalize($lead.FirstName.toLowerCase())##

#else

there##

#end

But there, too, you're saying that "AJ" and "B.B." aren't people's real-life first names.

Broderick_Klem1
Level 4

Re: Velocity Script - Clean Up First Name Field

Thanks! And yeah, it's definitely not perfect, but I think saying "Hi there," a to a few "AJ"s is an acceptable trade off for to not ever having obviously bad names, especially when the email is supposed to look like it's coming from a person.

Broderick_Klem1
Level 4

Re: Velocity Script - Clean Up First Name Field

Alright, I've got this working Almost perfectly, but I'm still running into one error:

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

  there##

#elseif($lead.FirstName.contains("@")) 

  there##

#elseif($lead.FirstName.contains("."))

  there##

#elseif($lead.FirstName.contains("1"))

  there##

#elseif($lead.FirstName.contains("2"))

  there##

#elseif($lead.FirstName.contains("3"))

  there##

#elseif($lead.FirstName.contains("4"))

  there##

#elseif($lead.FirstName.contains("5"))

  there##

#elseif($lead.FirstName.contains("6"))

  there##

#elseif($lead.FirstName.contains("7"))

  there##

#elseif($lead.FirstName.contains("8"))

  there##

#elseif($lead.FirstName.contains("9"))

  there##

#elseif($lead.FirstName.contains("0"))

  there##

#elseif($lead.FirstName.contains("_"))

  there##

#elseif($lead.FirstName.contains(" "))

  #set($name = $lead.FirstName.split(" ")[0])

    #if($name.length() > 1) 

    $display.capitalize($name.toLowerCase())##

    #else 

    there##

    #end

#elseif($lead.FirstName.length() < 2)

  there##

#else

  $display.capitalize($lead.FirstName.toLowerCase())##

#end

This is working perfectly, except lines 29-35, where it splits the name if there is a space and is supposed to just capitalize the subsequent name if its more than 1 character, or just use "there" if less than 1 character. However, it's adding a space to the end of it for some reason. So for example if the [lead.First Name] is "Sarah Baxter", then it would end up as "Hi Sarah ," (with a space between the name and comma).

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script - Clean Up First Name Field

Those conditionals are causing me physical pain.  The whole thing can be expressed in under 1/3 of the lines as:

#set( $FirstName = $lead.FirstName.trim() )

#if(

  $FirstName.isEmpty() ||

  $FirstName.matches(".*[0-9@._].*") ||

  $FirstName.length() < 2 ||

  $FirstName.indexOf(" ") == 1

)

there##

#else

#set( $FirstName = $FirstName.split(" +")[0] )

$display.capitalize($FirstName.toLowerCase())##

#end

If you need more than 3 if/else statements (in any language) you're probably going about things the wrong way.

Broderick_Klem1
Level 4

Re: Velocity Script - Clean Up First Name Field

Haha, I figured it was easier than that, just didn't know how. Thank you so much!