SOLVED

Re: Replacing last 2 letters of name using velocity script

Go to solution
SanfordWhiteman
Level 10 - Community Moderator

Re: Replacing last 2 letters of name using velocity script


What are the performance impacts of checking the map twice (once with the contains, and then once to get the swap characters)?


Effectively none, it’s an O(1) operation x 2.

PramodBasavanna
Level 2

Re: Replacing last 2 letters of name using velocity script

@Jo_Pitts1 @SanfordWhiteman 

 

I tried to use the new robust code that Sanford suggested, it's throwing an exception "Innovation of  method substring"

PramodBasavanna_0-1652684042089.png

 

I tried to replace it with Jo code and again it's throwing error the same above error, not sure how it complied (approved) when I did first time. 

 

 

Jo_Pitts1
Level 10 - Community Advisor

Re: Replacing last 2 letters of name using velocity script

@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