Re: German Salutation - Velocity Script

Gerard_Donnell4
Level 10

German Salutation - Velocity Script

I am completely new to Velocity Scripting and have been tasked with setting up German Salutation to auto populate.  Can you tell me if this would work or what is wrong with it. My email will basically have a {{My.Salutation}} at the top that will pipe in the salutation based on the script token below. Thanks in advance for any help.

#if (${lead.Salutation} == "")

Sehr geehrte/r Frau/Herr ${lead.last_Name}

#elseif

(${lead.Salutation} == "Herr")

Sehr geehrter Herr ${lead.last_Name}

#elseif

(${lead.Salutation} == "Frau")

Sehr geehrte Frau ${lead.last_Name}

#elseif

(${lead.Salutation} == "Herr Dr.")

Sehr geehrter Herr Dr. ${lead.last_Name}

#elseif

(${lead.Salutation} == "Frau Dr.")

Sehr geehrte Frau Dr. ${lead.last_Name}

#elseif

(${lead.Salutation} == "Herr Prof.")

Sehr geehrter Herr Prof. ${lead.last_Name}

#elseif

(${lead.Salutation} == "Frau Prof.")

Sehr geehrte Frau Prof. ${lead.last_Name}

#elseif

(${lead.Salutation} == "Herr Prof. Dr.")

Sehr geehrter Herr Prof. Dr. ${lead.last_Name}

#elseif

(${lead.Salutation} == "Frau Prof. Dr.")

Sehr geehrte Frau Prof. Dr. ${lead.last_Name}

#end

16 REPLIES 16
Gerard_Donnell4
Level 10

Re: German Salutation - Velocity Script

Sanford Whiteman​ Im hoping you could help me solve this. 

Thanks,

Gerard

Casey_Grimes
Level 10

Re: German Salutation - Velocity Script

So, just taking a cursory look at this, this script could be incredibly simplified to:

#set ($greetCheck = ${lead.Salutation})

#if ($greetCheck.contains('Herr'))

Sehr geehter $greetCheck ${lead.LastName}

#elseif ($greetCheck.contains('Frau'))

Sehr geehrte $greetCheck {lead.LastName}

#else

Sehr geehrte/r Frau/Herr ${lead.LastName}

#end

which should work just fine for you.

SanfordWhiteman
Level 10 - Community Moderator

Re: German Salutation - Velocity Script

It's not really a contains, though. It's like a startsWith\b, which needs a regex. Plus unknown Salutations aren't necessarily supposed to be output.

SanfordWhiteman
Level 10 - Community Moderator

Re: German Salutation - Velocity Script

You want to use a dictionary object, rather than a lot of conditions.  Also looks there's a lot of repetition here:

  • the salutation is present in the condition and then also printed exactly as is in the output
  • the difference is based on the first word being Herr vs Frau, not on the full salutation

So following the DRY (Don't Repeat Yourself) maxim, let's check only if the field starts with your interesting words, rather than a full match:

#set( $greetingsBySalutationStart = {

      "Herr" : "Sehr geehrter",

      "Frau" : "Sehr geehrte",

      "$" : "Sehr geehrte/r Frau/Herr"

})

## ---- NO NEED TO TOUCH ANYTHING BELOW THIS LINE! ----

#set( $greeting = $greetingsBySalutationStart["$"] )

#foreach( $startPattern in $greetingsBySalutationStart.keySet() )

#if( $lead.Salutation.matches("^${startPattern}\b.*") )

#set( $greeting = "${greetingsBySalutationStart[$startPattern]} ${lead.Salutation}" )

#end

#end

${greeting} ${lead.LastName}

Notes:

  • I used $lead.LastName because that's the Velocity name for Last Name in my instances. Does yours really use $lead.last_Name?
  • the index ["$"] is the default (you probably figured that out)
Gerard_Donnell4
Level 10

Re: German Salutation - Velocity Script

Hi Sanford Whiteman

So does the start of this script check if the Salutation contains "Herr" or "Frau" or if it equals "Herr" or "Frau"?  The reason Im asking is because our German forms actually have all 8 of the salutations mentioned above.

Thanks,

Gerard

  1. #set( $greetingsBySalutationStart = { 
  2.       "Herr" : "Sehr geehrter"
  3.       "Frau" : "Sehr geehrte"
  4.       "$" : "Sehr geehrte/r Frau/Herr" 
  5. }) 
SanfordWhiteman
Level 10 - Community Moderator

Re: German Salutation - Velocity Script

The object at the top (the dictionary) is of *first words* in the salutation.

It's used in the code with a regex (pattern match) fixed to the start of the string. So "Herr" will match just "Herr," and also "Herr Prof," "Herr Dr." or "Herr Anything." But it won't (purposely) match "Herry" or "John Herr" -- those would use the default greeting.

Gerard_Donnell4
Level 10

Re: German Salutation - Velocity Script

Hi Sanford Whiteman​,

For some reason I couldn't get that to work, it kept defaulting to the general salutation.  I managed to get the version below to work.  I based it off the documentation.  It is certainly not nice looking and isn't following DRY principles but it works for the meantime.

##check if the Salutation is Herr

#if(${lead.Salutation} == "Herr")

  ##if the Salutation is Herr, use the salutation 'Sehr geehrter Herr'

  #set($greeting = "Sehr geehrter Herr ${lead.LastName},")

##check is the Salutation is Frau

#elseif(${lead.Salutation} == "Frau")

  ##if female, use the salutation 'Sehr geehrte Frau'

  #set($greeting = "Sehr geehrte Frau ${lead.LastName},")

    ##check if the Salutation is Herr Dr.

#elseif(${lead.Salutation} == "Herr Dr.")

  ##if the Salutation is Herr Dr., use the salutation 'Sehr geehrter Herr'

  #set($greeting = "Sehr geehrter Herr Dr. ${lead.LastName},")

##check is the Salutation is Frau Dr.

#elseif(${lead.Salutation} == "Frau Dr.")

  ##if Frau Dr., use the salutation 'Sehr geehrte Frau Dr.'

  #set($greeting = "Sehr geehrte Frau Dr. ${lead.LastName},")

##check is the Salutation is Herr Prof.

#elseif(${lead.Salutation} == "Herr Prof.")

  ##if Herr Prof., use the salutation 'Sehr geehrter Herr Prof.'

  #set($greeting = "Sehr geehrter Herr Prof. ${lead.LastName},")

##check is the Salutation is Frau Prof.

#elseif(${lead.Salutation} == "Frau Prof.")

  ##if Frau Prof., use the salutation 'Sehr geehrte Frau Prof.'

  #set($greeting = "Sehr geehrte Frau Prof. ${lead.LastName},")   

##check is the Salutation is Herr Prof. Dr.

#elseif(${lead.Salutation} == "Herr Prof. Dr.")

  ##if Herr Prof. Dr., use the salutation 'Sehr geehrter Herr Prof. Dr.'

  #set($greeting = "Sehr geehrter Herr Prof. Dr. ${lead.LastName},")   

##check is the Salutation is Frau Prof. Dr.

#elseif(${lead.Salutation} == "Frau Prof. Dr.")

  ##if Frau Prof. Dr., use the salutation 'Sehr geehrte Frau Prof. Dr.'

  #set($greeting = "Sehr geehrte Frau Prof. Dr. ${lead.LastName},")

#else

  ##otherwise, use this Salutation

  #set($greeting = "Sehr geehrte/r Frau/Herr ${lead.FirstName},")

#end

##print the greeting and some content

${greeting}

SanfordWhiteman
Level 10 - Community Moderator

Re: German Salutation - Velocity Script

My code was tested thoroughly against representative test values... can't really comment on this monstrosity but I hope you will give another try for cleaner code later.

SanfordWhiteman
Level 10 - Community Moderator

Re: German Salutation - Velocity Script

Run this test:

#set( $testLead1 = {

  "LastName" : "Namor",

  "Salutation" : "Herr"

})

#set( $greetingsBySalutationStart = {

      "Herr" : "Sehr geehrter",

      "Frau" : "Sehr geehrte",

      "$" : "Sehr geehrte/r Frau/Herr"

})

#set( $greeting = $greetingsBySalutationStart["$"] )

#foreach( $startPattern in $greetingsBySalutationStart.keySet() )

#if( $testLead1.Salutation.matches("^${startPattern}\b.*") )

#set( $greeting = "${greetingsBySalutationStart[$startPattern]} ${testLead1.Salutation}" )

#end

#end

${greeting} ${testLead1.LastName}

#set( $testLead2 = {

  "LastName" : "Lastman",

  "Salutation" : "Herr Dr."

})

#set( $greetingsBySalutationStart = {

      "Herr" : "Sehr geehrter",

      "Frau" : "Sehr geehrte",

      "$" : "Sehr geehrte/r Frau/Herr"

})

#set( $greeting = $greetingsBySalutationStart["$"] )

#foreach( $startPattern in $greetingsBySalutationStart.keySet() )

#if( $testLead2.Salutation.matches("^${startPattern}\b.*") )

#set( $greeting = "${greetingsBySalutationStart[$startPattern]} ${testLead2.Salutation}" )

#end

#end

${greeting} ${testLead2.LastName}

#set( $testLead3 = {

  "LastName" : "Whiteman",

  "Salutation" : "Friar"

})

#set( $greetingsBySalutationStart = {

      "Herr" : "Sehr geehrter",

      "Frau" : "Sehr geehrte",

      "$" : "Sehr geehrte/r Frau/Herr"

})

#set( $greeting = $greetingsBySalutationStart["$"] )

#foreach( $startPattern in $greetingsBySalutationStart.keySet() )

#if( $testLead3.Salutation.matches("^${startPattern}\b.*") )

#set( $greeting = "${greetingsBySalutationStart[$startPattern]} ${testLead3.Salutation}" )

#end

#end

${greeting} ${testLead3.LastName}

#set( $testLead4 = {

  "LastName" : "Absent",

  "Salutation" : ""

})

#set( $greetingsBySalutationStart = {

      "Herr" : "Sehr geehrter",

      "Frau" : "Sehr geehrte",

      "$" : "Sehr geehrte/r Frau/Herr"

})

#set( $greeting = $greetingsBySalutationStart["$"] )

#foreach( $startPattern in $greetingsBySalutationStart.keySet() )

#if( $testLead4.Salutation.matches("^${startPattern}\b.*") )

#set( $greeting = "${greetingsBySalutationStart[$startPattern]} ${testLead4.Salutation}" )

#end

#end

${greeting} ${testLead4.LastName}

Expected (and observed on my primary instance) output:

Sehr geehrter Herr Namor

Sehr geehrter Herr Dr. Lastman

Sehr geehrte/r Frau/Herr Whiteman

Sehr geehrte/r Frau/Herr Absent