SOLVED

Re: Replacing last 2 letters of name using velocity script

Go to solution
PramodBasavanna
Level 2

Replacing last 2 letters of name using velocity script

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

Accepted Solutions
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

 

View solution in original post

12 REPLIES 12
PramodBasavanna
Level 2

Re: Replacing last 2 letters of name using velocity script

Hi,

 

I tried to solve it with below script and it worked.

#set( $Name = ${lead.FirstName} )
#set( $FormatName = $Name .trim().replaceAll("(?i),?(as\.?)${esc.d}", "ai") )
${FormatName }

 

Now, I am looking forward for your help to solve below conditions in one script.

  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

@SanfordWhiteman Please help!

Jo_Pitts1
Level 10 - Community Advisor

Re: Replacing last 2 letters of name using velocity script

@PramodBasavanna ,

try this.  There is probably a shorter more elegant version.  However, the verbosity of this is in some ways useful

 

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

#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) ))
#set( $swapChars = $nameEnding[$lastChars].updated ) 

#if(!$swapChars.length().equals(0))
  #set($outputName = "$originalLastname.substring(0,$math.sub($originalLastname.length(),$checkChars))$swapChars")
#else
  #set($outputName = $originalLastname)
#end

${outputName}

 

Here's how it all works:

The first section sets up a map that is readily searchable.

Jo_Pitts1_6-1652042082158.png

 

Then I set swapChars to be an empty (but defined) string, and the number of characters to look for at the end of the name to 2

Jo_Pitts1_7-1652042107748.png

I put the leads last name into a local variable, and test to see if it ends in ė.  If it does, I reduce set CheckChars to 1

Jo_Pitts1_8-1652042177310.png

I get the correct length of ending  into the lastChars variable, and then search the map to and put the output (if any) into swapChars

Jo_Pitts1_9-1652042238162.png

If there is anything in $swapChars I take all of the last name, minus the length of the swappable section, and add in the replacement chars

Jo_Pitts1_10-1652042295195.png

and then output it.

Jo_Pitts1_5-1652041165703.png

Cheers

Jo

 

PramodBasavanna
Level 2

Re: Replacing last 2 letters of name using velocity script

@Jo_Pitts1 Thanks a ton! Script is working as intended, the you a way explained helps us to learn and improve our coding skills.  

 

Cheers

Pramod

SanfordWhiteman
Level 10 - Community Moderator

Re: Replacing last 2 letters of name using velocity script

I don’t like how what would be a fatal error (were it not for Velocity coercing the error to falsy) is swallowed here.

 

Better:

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

#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}

 

Jo_Pitts1
Level 10 - Community Advisor

Re: Replacing last 2 letters of name using velocity script

@SanfordWhiteman ,

now I'm curious:

Why is this

#if(!$swapChars.length().equals(0))

considered falsey?  It has a boolean in it (the .equals() ), and I have (in my inimitable old school way) set $swapChars to be "" initially, so it has a 'length'.

 

Cheers

Jo 

SanfordWhiteman
Level 10 - Community Moderator

Re: Replacing last 2 letters of name using velocity script

It’s not actually that line but more here, where you rely on Velocity skipping the error (and not assigning the variable) instead of throwing:

$nameEnding[$lastChars].updated

 

Jo_Pitts1
Level 10 - Community Advisor

Re: Replacing last 2 letters of name using velocity script

Ah. gotcha.  So there is a risk of Velocity throwing an error when assigning if nothing is found in the map?

SanfordWhiteman
Level 10 - Community Moderator

Re: Replacing last 2 letters of name using velocity script

In this exact code there’s no chance that Velocity will throw a fatal error, but similar code like referencing a nonexistent array index will throw.

 

So it’s best to treat Velocity as if it’s Java because it’s impossible to know (without checking the Apache source code constantly) when you’ve accidentally created a non-swallowed error.

Jo_Pitts1
Level 10 - Community Advisor

Re: Replacing last 2 letters of name using velocity script

Gotcha.  

Thanks for making my code a bit more robust :).

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

Cheers

Jo