5 Replies Latest reply on Mar 5, 2018 2:11 PM by Broderick Klemetson

    Velocity Script - Clean Up First Name Field

    Broderick Klemetson

      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?

        • Re: Velocity Script - Clean Up First Name Field
          Sanford Whiteman

          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.

            • Re: Velocity Script - Clean Up First Name Field
              Broderick Klemetson

              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.

              • Re: Velocity Script - Clean Up First Name Field
                Broderick Klemetson

                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).