Advanced Dynamic Content – If Statements

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. We’ve shown how to use freemarker in a previous post, and this post will go into more advanced details. Our previous post covers the situation in which the fields you are referencing will contain data for all recipients. While it would be best if everyone had all of their information filled in, 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 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”.

Start with an opening assign statement <#assign language= then use the personalization dropdowns to insert the fields you want to check and then edit them for best results.

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

Now remove the ${ and the closing } from what was just inserted. Then choose a filler value if the language field is blank on the contact. The !”” part of the code means “if there is no value use what is in these quotes”. We will choose the word “null” as our filler value. Then a backslash (/) and a greater than sign (>). 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.

Do the same for the gender, so it will now look like this:

Now it is time to check the contacts’ records. Add the first if statement with <#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 actually “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″>

Then add the content that a Spanish Male would see. After that we check if the recipient is a female. And so on.

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 close 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, we will put an open else statement:


Other than this line, the rest is the same:

Here’s the whole thing:

Always, always, always test before you send something with if statement Freemarker. 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. You can see the render error(s) by going to the Email Events on the Email Send. You can see more details by opening up the render error to see the message of what is wrong.

Powered by WPeMatico