NOTE: This is an update to an earlier post on the same topic.

Do you want to create an email that displays different content based on a field in the recipient’s contact record? You can do that with FreeMarker. While it would be best if the fields you are referencing contained data for all recipients, that’s just not always the case. When one of the fields you are referencing does not contain data, the email will not be sent to that person at all. In this post, we will solve that problem by using variables in FreeMarker. We will continue with a similar scenario of referencing the language and gender fields in a contact record. Here’s what the logic looks like:

  • If the recipient speaks Spanish…
    • If the recipient is male…
      • Content for male Spanish speakers goes here
    • If the recipient is female…
      • Content for female Spanish speakers goes here
    • If the recipient’s gender is not set
      • Content for unknown gender
    • Content for all Spanish speakers goes here
  • If the recipient speaks English, another language, or does not have a language set…
    • If the recipient is male…
      • Content for male English speakers goes here
    • If the recipient is female…
      • Content for female English speakers goes here
    • If the recipient’s gender is not set
      • Content for unknown gender
    • Content for all English speakers goes here
  • Content for all recipients goes here

If a field doesn’t contain data, FreeMarker will not have a value to compare in the ‘if’ statement and it will produce a render error. Therefore, we need to create a variable and assign it a value within FreeMarker.

In our example, we are not sure if everyone will have data in the gender or language fields. So we need to create two variables; we will use “gender” and “language.”

We will start with a FreeMarker assign statement. This will allow us to define variables that we can use in the place of the actual FreeMarker dynamic content. For example, we will be creating the variable “language,” which will be used in place of the full FreeMarker text: ${[0]!””}. Start with an opening assign statement then use the personalization drop-downs to insert the fields you want to check. In our example, we now have:

<#assign language=${[0]!””}

TIP: Did you create a custom field and you don’t see it in the drop-down? You need to publish Metadata.

Next, we will need to make a few adjustments to the FreeMarker syntax. Remove the ${ and the closing } from the FreeMarker that was just inserted and then choose a filler value to use if the language field is blank on the contact. The !”” part of the code means “if there is no data in the referenced field, use what is in these quotes instead.” We will choose the word “null” as our filler value and add a backslash (/) and a greater than sign (>) to close out the assign statement. Make sure it looks just like this (notice there are no spaces between the end symbols “/> or around the equals sign):


What the above statement will do is set “language” equal to the string in the language field for this particular contact (for example “English”), but if there is no value in the language field, it will set it equal to “null.” This way we always have a value and the emails will always go out.

TIP: For best results, do not leave any spaces around the equal signs.

Repeat the process to set up a second variable for gender, so it will now look like this:

Now we will set up a series of conditional (if) statements that will determine which content the recipient sees. Begin by adding the first “if” statement as <#if language==”Spanish”>

NOTICE: When we assign something, there is just one = sign, but when use an “if” statement there are two = signs.

The gender field is an option set, not a text field. In any case where the field is a picklist (has limited options), we need to find what the value for the option is, not the label. You can do this by going to Customizations > Customize the System > Contact > Fields and opening the correct field. For this example, we can see that the value for male is “1,” so we will use that in our FreeMarker.

We now have this:

<#assign[0]!”null”/><#assign[0]!”null”/><#if language==”Spanish”><#if gender==”1″>

Now add the content that a Spanish-speaking male would see. Then we check if the recipient is a female. And so on. When the email is sent, the recipient will only see the content that comes after the “if” statement that is applicable to them (for example, Spanish-speaking male) and before the subsequent “if” statement that checks for different conditions (Spanish-speaking female).

We are almost done with the first section. We just need to close the inner “if” statement. Right now we have two statements open (if the language is Spanish, and if the gender is male, female or null). We just want to close the “if” gender statement right now. A closed “if” statement looks like this:


Now we will check for anyone who speaks English, does not have a language, or speaks a language other than English or Spanish. This in essence means “any language other than Spanish.” In coding terms, this means we need an “else” statement.

Right after the last closing “if” tag, we will put an open “else” statement:


Other than this line, the rest is the same:

Here’s the whole template:

Be sure to always test before you send something with FreeMarker “if” statements. Add yourself to an email send as a recipient and make sure it works correctly. If you do not receive the email, there was probably a render error due to incorrectly formatted dynamic content. You can see the render error(s) by going to Email Events on the Email Send within CRM. You can see more details by opening up the render error.

If you get a render error, you can fix the listed error in your email template and send the email to yourself again. If you are unsure how to fix it or do not understand the error message, feel free to reach out to us for assistance!

Written by Rhys Saraceni, ClickDimensions Marketing Success Manager Team Lead