Using an If statement in an email template

Hello,

I am trying to use an if statement in the “Certification” email template, but the conditional does not seem to be working as it should. For example, this is the body of the email:
emailtest

And this is the email that I receive:

The value of “user.band” is 700
Fail
Fail
Fail

So, for some reason 700 != 700. If anybody has any idea where I am going wrong, please let me know.

One thing I notice is the spaces. If your statement were to succeed, you would receive an output of " Success " and also the failed statement would result in " Fail " with spaces on either side; I’m not sure if this is by design or not.

Regardless of that point, I think that you actually have to set a local variable for these value references within email templates. I was able to achieve this myself. Here is an example that can be applied to your situation:

#set($bandValue = ${user.band})#if($bandValue=='700')Success#{else}Fail#end

Let me know if this solution works for you!

Thank you for your response, unfortunately that did not work. This was what I put in the email template:

And the result was:
Fail
$bandValue

So it looks like $bandValue was not set to any value. I’ll keep trying.

@jwinans12

Interesting. Will you also try entering ${user.band} in the template to see what the value of that is?

Also… did you generate this from the “Test Email” option in the template editor? If so, that would be the reason why it didn’t send the value. When you use the “Test Email” option, there is no “user” object to reference, which means all of those variables are null.

You have to actually test it with a real event that would generate the email. Can you try that out? Let me know how it goes.

  1. If you look at the first screenshot, you can see that the value of ${user.band} is 700; but yet, for some reason, the three if statements in that screenshots all fail. It makes no sense.
  2. No, I have not used the Test Email option, I start off an actual certification campaign each time.

So, in some cases user.band = 700 & other times it doesn’t? It does not make sense.

Thank you for your help, let me know if you have any more suggestions!

According to the Apache Velocity Template specification, you shouldn’t need the curly braces when using variables in your if/else. I was able to successfully apply an if statement in the subject line, which you can find in this topic.

Maybe try this:

The value of "user.band" is ${user.band}

#if( $user.band == '700')Success#{else}Fail#end

Hi Colin,

I did what you suggested, and I also included the same if statement as the subject line.
emailtest3

The result:

Subject:

Fail

Body:

The value of “user.band” is 700
Fail

So I think the title of this topic is not entirely accurate, because an if statement works as it should in other situations. I think the issue arises when comparing a variable to a literal. For example, I also tried this:

The value of “user.country” is ${user.country}
#if( $user.country == “Israel” )Success#{else}Fail#end

Resulting in:
The value of “user.country” is Israel
Fail

For the record, in the “Lifecycle State Change” email template, this was not an issue. This issue has come up in the “Certification” & “Certification Reassignment” email templates so far.

What is the type of user.band? Is it a string or an int? The type should be defined in your account schema. This could be a type mismatch, leading to an always false result, but your subsequent code with the user.country suggests otherwise.

I ask because I just tested my Certification email template using the following logic, and I got a success:

Dear ${user.name},

#if( $user.name == "Colin McKibben")Success#{else}Fail#end

Something else you might try, just to rule out an issue with Velocity, is compare the variable to itself. For example:

The value of “user.country” is ${user.country}
#if( $user.country == $user.country )Success#{else}Fail#end

If this succeeds, then we need to inspect the value of user.country closer. Maybe there is some trailing white space or other hidden character that we aren’t seeing.

So “band” is an identity attribute that is sourced from an authoritative source and is a string. (same for “country”)

Also, ( $user.country == $user.country ) does evaluate to true.

are you applying any transforms to band and country? If so, what transforms?

Another option you can try is to use the Java String.equals() method instead of ==. Since Apache Velocity is really just a template language on top of Java, you can use some Java methods. Can you try this:

The value of “user.country” is ${user.country}
#if( $user.country.equals("Israel") )Success#{else}Fail#end

Unfortunately using the .equals() method also failed.

Let’s dig deeper…

What is the class type and length of your $user.country?

${user.country.getClass().getName()}
${user.country.length()}

So that query, for ${user.country} = Israel, returns:

java.lang.String
6

For a sanity check, can you provide the output of the following code?

${user.country.getClass().getName()}
${user.country.length()}

#set($country = "Israel")

The value of “user.country” is ${user.country}
The value of "country" is ${country}

#if( $user.country.getClass().getName() == "java.lang.String" )Success#{else}Fail#end

#if( $user.country.equals("Israel") )Success#{else}Fail#end
#if( $user.country == $country )Success#{else}Fail#end

#if( $user.country == $user.country )Success#{else}Fail#end

${user.country.hashCode()}
${country.hashCode()}

#if($user.country.hashCode() == $country.hashCode())Success#{else}Fail#end

Here you go:

image

Try doing the same, but this time send it as a test email. Make sure to set your email config to send all emails to a test address, which would be your email. I’ve been testing this on my end using test emails, and it works. i wonder if there is a difference in outcomes between a real event and a test event.

Well, since its a test email there is no “user” so this is the result:
image

Ah, I’ve been testing with ${user.name}, which appears to be the only variable that works. Are you able to confirm that you can compare variables in other templates just fine? Is it really just the Certification template where this is an issue?

I have tried the “Certification” template, the “Certification Reassignment” template, and the “Access Request for Other” template, all with the same result. My colleague, with your help (in the link you sent earlier) , was able to do this type of thing successfully in the “Lifecycle State Change” template.

I’m not sure if this is why, but that template was able to use identity.country or identity.band instead of user.country or user.band