Re: German Salutation - Velocity Script

Gerard_Donnell4
Level 10

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

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

Thanks,

Gerard

SanfordWhiteman
Level 10 - Community Moderator

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

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

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

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

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

Dragan_Bodrozic
Level 1

Hi Sanford Whiteman

I really want to implement your solution because it's scalable and the code is much cleaner than using if/then statements.

Just like Gerard, I was able to make the long if/then version work but I can't make yours work (with the dictionary/regex) - it always outputs the default value.

I run your test above and got the same result - default value.

  • Checked Last Name and Salutation lead variables in the script editor to make sure they are visible to Velocity.
  • Tested with a lead that has "Mr." value for the Salutation.
  • Checked the script syntax.
  • Checked the field names - they match what you have in your script
  • The test email is part of the program that has the token

What else could I check for?

SanfordWhiteman
Level 10 - Community Moderator

Are you using Send Sample, or a real email? You must send a real email.

You did change the $testLeadN stuff to $lead, right?

Dragan_Bodrozic
Level 1

Yes, I use ${lead.LastName}.

I was using test email but I just tried the real email and the result is the same.

Not sure why I would even have to send an email - I should be able to preview the result by selecting "View By" lead which works for the long if/then Velocity statement.

SanfordWhiteman
Level 10 - Community Moderator

If you do Preview-by-List, yes, you don't have to send an email. But if you do send an email, it can't be a sample.

Please show the full code you're using in both cases (highlighted as Java using the Advanced Editor's syntax highlighter).

Dragan_Bodrozic
Level 1

Code that always outputs the default value:

#set( $greetingsBySalutationStart = {

"Mr." : "Sehr geehrter Herr",

"Mrs." : "Sehr geehrte Frau",

"$" : "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}

Code that works:

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

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

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

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

#else

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

#end

${greeting}

SanfordWhiteman
Level 10 - Community Moderator

Because when you include the . at the end, it no longer matches the word break \b.

Try this:

#set( $greetingsBySalutationStart = {

"Mr\.?" : "Sehr geehrter Herr",

"Mrs\.?" : "Sehr geehrte Frau",

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

})

Dragan_Bodrozic
Level 1

Of course! Little things that mean a lot in regex world

Thanks so much Sanford and hopefully this post helps a lot of people that are trying to figure out the same thing.

SanfordWhiteman
Level 10 - Community Moderator

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.

Casey_Grimes
Level 10

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

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.