SOLVED

Replacing last 2 letters of name using velocity script

Go to solution
PramodBasavanna
Level 2

Hi Champions,

I want to write a script to replace last two letters of the first name, I have 5 conditions to solve here. 

  1. If name has ending -as, then salutation ends – ai.  Example: Rokas – Rokai, Mindaugas – Mindaugai
  2. if ending -is, then salutation ends -i.  Example: Rytis – Ryti, Kęstutis – Kęstuti
  3. if ending -ys, salutation -y. Example: Balys – Baly, Kazys – Kazy
  4. If ending -us, salutation ends -au. Example: Paulius – Pauliau, Baltrus – Baltrau
  5. If ending -ė, salutation ends -e. Example: Auksė – Aukse, Kamilė – Kamile

I am trying to decode it step by step and written below script to  solve condition 1 (Replace as with ai)

 

#set( $Name = ${lead.FirstName} )
#set( $FormatName = $Name.trim().replaceAll("(.*)as", "ai") )
${FormatName}

 

The above script is replacing whole word instead of just "as", any help here is much appreciated. 

 

@SanfordWhiteman 

1 ACCEPTED SOLUTION
Jo_Pitts1
Level 10 - Community Advisor

@PramodBasavanna 

try this

#set( $nameEnding = {
  "as" : {
    "updated":"ai"
  },
  "is" : {
    "updated":"i"
  },
  "ys" : {
    "updated":"y"
  },
  "us" : {
    "updated":"au"
  },
  "ė" : {
    "updated":"e"
  }
}
)  
#set( $swapChars="" )
#set( $checkChars=2 )

#if( ! $display.alt($lead.LastName,"").isEmpty() )
  #set( $originalLastname = $lead.LastName )
  #if ( $originalLastname.substring( $math.sub($originalLastname.length(),1) ).equals("ė"))
    #set( $checkChars=1 )
  #end

  #set( $lastChars = $originalLastname.substring( 
  $math.sub($originalLastname.length(),$checkChars) ))

  #if( $nameEnding.containsKey($lastChars) )
    #set($outputName = 
"${originalLastname.substring(0,$math.sub($originalLastname.length(),$checkChars))}${nameEnding[$lastChars].updated}")
  #else
    #set($outputName = $originalLastname)
  #end

${outputName}
#end

I added in this line

#if( ! $display.alt($lead.LastName,"").isEmpty() )

to test to see if it has a real lead to work with.  When you test an email, something can work in velocity, but when you approve the email, it'll fail as it has no lead data to actually work with.

 

Cheers

Jo

 

View solution in original post

12 REPLIES 12