SOLVED

Re: Velocity token - salutation emails

Go to solution
Tom_Liolios4
Level 4

Hi all,

I would like to get some help with setting up a velocity script for a program token that should display the correct salutation in my German emails

--> In German, you need to differentiate between male and females in the salutation, and this also has an effect on how you write certain words.

I have absolutely no experience with creating such scripts, so it would be great if someone could easily explain what kind of script I should use.

I found the following script on http://developers.marketo.com/email-scripting/ . I copied it, and edited the tokens for the ones I use in my instance.

I made sure that the checkboxes for the necessary tokens are checked in the token editor.

Could someone look at this script, and provide any adjustments, as this is not working now.

##check if the lead is male

#if(${lead.new_gender} == "Male")

  ##if the lead is male

  #set(${my.GermanSalutation} = "Sehr geehrter Herr ${lead.Full_Name},")

##check is the lead is female

#elseif(${lead.new_gender} == "Female")

  ##if female

  #set(${my.GermanSalutation} = "Sehr geehrte Frau ${lead.Full_Name},")

#else

  ##otherwise, use the first name

  #set(${my.GermanSalutation} = "Sehr geehrte/r Frau/Herr ${lead.Full_Name},")

#end

Appreciate all the help!

Thanks,

Tom

1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator

Thanks Dory​... Tom there's nothing inherently wrong about the way you're generating the salutations, but I think you're misunderstanding how Velocity output works (but you should always detail what error/behavior you're seeing -- "not working" is usually a recipe for a response like "what do you mean by 'not working'").

For output, you don't want to #set anything, but the opposite: lines that don't begin with # become the value of the {{my.token}}.  So if you have one line of output like

Sehr geehrter...

Then "Sehr geehrter" is the value of the {{my.token}} when inserted.

I would also advise using a more maintainable code structure when doing translation/dictionary/localization patterns like this. Separate your dictionary from the output itself. Allow for a range of languages later. And also don't use curly braces on #set lines where they aren't necessary and make the code more fragile.

#set( $currentLang = "de" )

#set( $salutations = {

  "de" : {

    "male" : "Sehr geehrter Herr ${lead.Full_Name}",

    "female" : "Sehr geehrte Frau ${lead.Full_Name}",

    "" : "Sehr geehrte/r Frau/Herr ${lead.Full_Name}"

  }

} )

#if( $salutations[$currentLang].containsKey($lead.new_gender) )

${salutations[$currentLang][$lead.new_gender]}

#else

${salutations[$currentLang][""]}

#end

Note if the field New Gender in Marketo is only ever "male," "female," or empty, you don't need the extra failsafe condition at the end and can just use

#set( $currentLang = "de" )

#set( $salutations = {

  "de" : {

    "male" : "Sehr geehrter Herr ${lead.Full_Name}",

    "female" : "Sehr geehrte Frau ${lead.Full_Name}",

    "" : "Sehr geehrte/r Frau/Herr ${lead.Full_Name}"

  }

} )

${salutations[$currentLang][$lead.new_gender]}

View solution in original post

14 REPLIES 14
SanfordWhiteman
Level 10 - Community Moderator

Thanks Dory​... Tom there's nothing inherently wrong about the way you're generating the salutations, but I think you're misunderstanding how Velocity output works (but you should always detail what error/behavior you're seeing -- "not working" is usually a recipe for a response like "what do you mean by 'not working'").

For output, you don't want to #set anything, but the opposite: lines that don't begin with # become the value of the {{my.token}}.  So if you have one line of output like

Sehr geehrter...

Then "Sehr geehrter" is the value of the {{my.token}} when inserted.

I would also advise using a more maintainable code structure when doing translation/dictionary/localization patterns like this. Separate your dictionary from the output itself. Allow for a range of languages later. And also don't use curly braces on #set lines where they aren't necessary and make the code more fragile.

#set( $currentLang = "de" )

#set( $salutations = {

  "de" : {

    "male" : "Sehr geehrter Herr ${lead.Full_Name}",

    "female" : "Sehr geehrte Frau ${lead.Full_Name}",

    "" : "Sehr geehrte/r Frau/Herr ${lead.Full_Name}"

  }

} )

#if( $salutations[$currentLang].containsKey($lead.new_gender) )

${salutations[$currentLang][$lead.new_gender]}

#else

${salutations[$currentLang][""]}

#end

Note if the field New Gender in Marketo is only ever "male," "female," or empty, you don't need the extra failsafe condition at the end and can just use

#set( $currentLang = "de" )

#set( $salutations = {

  "de" : {

    "male" : "Sehr geehrter Herr ${lead.Full_Name}",

    "female" : "Sehr geehrte Frau ${lead.Full_Name}",

    "" : "Sehr geehrte/r Frau/Herr ${lead.Full_Name}"

  }

} )

${salutations[$currentLang][$lead.new_gender]}

Silvia_Schlatte
Level 1

@SanfordWhiteman You're script is awesome. I am wondering how we could merge different languages into one script / token,

if we are using dynamic content and language segments in our emails.

 

Currently, we do have set up one token per language... but this is very prone to error. If you do forget to change the token in the dynamic content. Could you help with this? 

SanfordWhiteman
Level 10 - Community Moderator

Sure, the code is already set up for that – it's supposed to be one shared single object ("{}" in Velocity creates an object, technically a map object) with a key (named property) for each language, i.e.:

 

#set( $salutations = { 
  "nl" : { 
    "Male" : "Beste meneer ${lead.LastName},", 
    "Female" : "Beste mevrouw ${lead.LastName},", 
    "" : "Beste ${lead.Full_Name}," 
  },
  "fr" : { 
    "Male" : "Monsieur ${lead.LastName},", 
    "Female" : "Madame ${lead.LastName},", 
    "" : "Bonjour ${lead.Full_Name}," 
  } 
} ) 

 

Tom_Liolios4
Level 4

Hi Sanford Whiteman​,

Thanks a lot for your help. I agree, saying that it is 'not working' is not that clear.

To be exact, I inserted the {{my.token}} in an email and sent it to a lead with the new_gender field set to male. The email came back blank.

I get what you're saying with allowing for more languages later. However, we're about to launch a campaign in Germany, so I need to get this working quite quickly. I only have these fields to my availability:

  • lead.new_gender  (which is always male, female or empty)
  • lead.Full_Name (which should be part of the output --> "Sehr geehrte Frau [Full Name] or Sehr geehrter Herr [Full Name]
  • and of course I have the {{my.token}}, which I named my.GermanSalutation

Would you be able to help me out with a code that works with these fields?

Appreciate it!

Thanks,

Tom

SanfordWhiteman
Level 10 - Community Moderator

You don't have to refactor your code that much for it to work, but what you're missing is you don't #set the my.token *in the script*. All your output lines (any line that *doesn't* start with #) concatenated together automatically become the token value.

See the last line of my second snippet? That's an output line. It wouldn't matter what I had called the my.token: that token name is used *outside* the VTL code itself. Whatever I output from the code becomes the token value. If the script token is first called {{my.saluto}} and I change it to {{my.schmaluto}}, the value remains the same, without making any change to the code inside the script token at all. (You would of course change the token name in the email to pick up the new content.)

Tom_Liolios4
Level 4

Hi Sanford,

It worked! My email with the salutation token now comes back with "Sehr geehrter Herr Tom Liolios"

I do not yet use the #set( $currentLang = "de" ) part in my instance, but it doesn't hurt if I leave it in the script for now right?

Thanks so much!

Katia_Piton
Level 2

Dear Tom, could you please share your latest script (the one which works ) with us please? I have the same one to implement in French ... many thanks!

Tom_Liolios4
Level 4

Hi Guillaume, apologies for the belated reply. Hereby the code I'm using for salutation. This is the example for Dutch, but you can easily adjust for any language.

Note: do not forget to link the corresponding fields in the selection menu on the right when setting up the velocity script.

#set( $currentLang = "nl" )

#set( $salutations = {

  "nl" : {

    "Male" : "Beste meneer ${lead.LastName},",

    "Female" : "Beste mevrouw ${lead.LastName},",

    "" : "Beste ${lead.Full_Name},"

  }

} )

#if( $salutations[$currentLang].containsKey(${lead.Gender__c}) )

${salutations[$currentLang][${lead.Gender__c}]}

#else

${salutations[$currentLang][""]}

#end

SanfordWhiteman
Level 10 - Community Moderator

Tom, better to take out the ${formal} notation (extra curly braces) unless you're inside double quotes or on an output-only line:

#set( $currentLang = "nl" )

#set( $salutations = {

  "nl" : {

    "Male" : "Beste meneer ${lead.LastName},",

    "Female" : "Beste mevrouw ${lead.LastName},",

    "" : "Beste ${lead.Full_Name},"

  }

} )

#if( $salutations[$currentLang].containsKey($lead.Gender__c) )

${salutations[$currentLang][$lead.Gender__c]}

#else

${salutations[$currentLang][""]}

#end

(In case anyone is wondering, the logic is not limited to binary male/female, it just happens to have only those plus the default/empty string "" listed at present.)

SanfordWhiteman
Level 10 - Community Moderator

Great!  And yes, you can leave that line in there -- no harm.  I have a blog post on translations in Velocity coming out soon that I'll link to here.  Meantime, if you could please mark my answer w/the code ​Correct.

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. 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

SanfordWhiteman
Level 10 - Community Moderator

If you open another thread I'm happy to answer.

Gerard_Donnell4
Level 10

Hi,

Thanks a lot.  I just opened a new thread and tagged you in it.

German Salutation - Velocity Script

Kind regards,

Gerard

Dory_Viscoglio
Level 10