SOLVED

Re: Velocity script to empty content based on value

Go to solution
Megan_Koelemay
Level 4

Velocity script to empty content based on value

I want to include some personalized content in my email that says Member since: XXXX where XXXX=year (contained in a data field).  And I want to populate the year if the field value is 2007 or later.  For anything earlier than 2007, I want to hide Member since: XXXX altogether.  

 

I'm using a Velocity script token in the email, but I don't know much about writing scripts and I don't know what to do on the second line below to hide the content.  I just guessed null() but it's not working.  Any ideas?

#if ($lead.MemberSince < 2007)
null()
#else
Member since: $lead.MemberSince
#end

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script to empty content based on value

#if ($lead.MemberSince >= 2007)
Member since: $lead.MemberSince
#end

 

Just don't output anything if the condition doesn't match.

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script to empty content based on value

You shouldn't use the == operator, only $myVariable.equals($myOtherVariable). (The double-equals is broken in very hard-to-debug ways.)

 

You're dealing with a String, not a Number. So:

#if ( $convert.toNumber($lead.MemberSince) <= 2007 )
Member since: $lead.MemberSince
#end

 

View solution in original post

8 REPLIES 8
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script to empty content based on value

#if ($lead.MemberSince >= 2007)
Member since: $lead.MemberSince
#end

 

Just don't output anything if the condition doesn't match.

Megan_Koelemay
Level 4

Re: Velocity script to empty content based on value

Thank you!  This makes sense, but I tried it and it shows up blank no matter what the year value is.  My token within the email is {{my.membersince}}, and I sent test emails using leads with values like 2009 and 2015.  

Megan_Koelemay
Level 4

Re: Velocity script to empty content based on value

After some testing, I can get things to "work" if I change the script to == 2007.  Examples:

 

For someone whose field value is 2007, it shows up as Member since: 2007

For someone whose field value is 2004, it shows up blank.

For someone whose field value is 2015, it shows up blank. 

 

#if ($lead.MemberSince == 2007)
Member since: $lead.MemberSince
#end

 

Not sure why the >= operator symbol won't work!  

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script to empty content based on value

You shouldn't use the == operator, only $myVariable.equals($myOtherVariable). (The double-equals is broken in very hard-to-debug ways.)

 

You're dealing with a String, not a Number. So:

#if ( $convert.toNumber($lead.MemberSince) <= 2007 )
Member since: $lead.MemberSince
#end

 

Megan_Koelemay
Level 4

Re: Velocity script to empty content based on value

Worked like a charm, just had to change <= to >=. 

 

Thanks so much, @SanfordWhiteman!

Megan_Koelemay
Level 4

Re: Velocity script to empty content based on value

Now I'm hoping to configure my token as a whole section of HTML content so it wouldn't leave an extra space if the condition doesn't match (a la this post - though it notes unintentional line breaks can happen, which would defeat the purpose). 

 

But I tried it and ran some Email on Acid tests, and not only am I seeing an extra space in some environments (like Outlook - no surprise there), but I'm also running into wonky formatting in quite a few environments (iPhone, Outlook, most web versions).  

 

Should I not be using meta tag references like background-color:${contentblockbackgroundcolor} or padding and whatnot in my Velocity scripts?  Here's what my email testing is showing - greenish background, padding issue and font issue:

Megan_Koelemay_2-1596506195322.png

 

And here's my updated Velocity script:

#if ( $convert.toNumber($lead.HCMemberSince) >= 2007 ) 
                              <tr> 
                                <td width="100%" align="left" valign="top" bgcolor="${contentblockbackgroundcolor}" style="width:100%;background-color:${contentblockbackgroundcolor};padding-top:2px;padding-bottom:2px;padding-left:30px;color:${contenttextcolor};font-size:14px;line-height:18px;font-family: Tahoma, Lucida Sans, sans-serif; text-align: left;mso-line-height-rule:exactly;-webkit-text-size-adjust:none;" class="rewardstext"> 
                                  <div class="mktEditable" id="MemberSinceSubhead_RewardsSummaryLeft" mktoname="MemberSinceSubhead_RewardsSummary">
                                    <span style="color: ${contenttextcolor}; font-size: 14px; line-height: 18px; font-family: Tahoma, Lucida Sans, sans-serif; text-align: left; mso-line-height-rule: exactly; -webkit-text-size-adjust: none;" class="rewardstext"><strong>Member since:</strong> ${lead.HCMemberSince}<br /></span>
                                  </div></td> 
                              </tr>
#end

 

Megan_Koelemay
Level 4

Re: Velocity script to empty content based on value

Update: It turns out my variables were causing the issue.  These are defined in the <head> of my email using <meta> tags.  Once I removed them and used inline styles instead, the formatting looked fine.  For example:

 

background-color:${contentblockbackgroundcolor}

was replaced with

background-color:#EFEFEF

 

There's still the issue of an extra line break in some environments (Outlook in particular), but that's better than having it in all of them.

 

Here's what my Velocity script looked like after the fix:

 

 

#if ( $convert.toNumber($lead.HCMemberSince) >= 2007 ) 
                              <tr> 
                                <td width="100%" align="left" valign="top" bgcolor="#EBEBEB" style="width:100%;background-color:#EBEBEB;padding-top:2px;padding-bottom:2px;padding-left:30px;color:#494949;font-size:14px;line-height:18px;font-family: Tahoma, Lucida Sans, sans-serif; text-align: left;mso-line-height-rule:exactly;-webkit-text-size-adjust:none;" class="rewardstext"> 
                                  <div id="MemberSinceSubhead_RewardsSummaryLeft" mktoname="MemberSinceSubhead_RewardsSummary">
                                    <span style="color: #494949; font-size: 14px; line-height: 18px; font-family: Tahoma, Lucida Sans, sans-serif; text-align: left; mso-line-height-rule: exactly; -webkit-text-size-adjust: none;" class="rewardstext"><strong>Member since:</strong> ${lead.HCMemberSince}<br /></span>
                                  </div></td> 
                              </tr>
#end

 

 

 

 

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script to empty content based on value

A mktoVariable isn't supported in Velocity, so that wouldn't work in any case.

 

As far as extra line breaks — Velocity outputs everything you tell it to, including whitespace. So at the end of the code you've just provided, there's a trailing line break. If you want to suppress that, the #end needs to be on the line before, or you can end the line before with ## (two hashes) to comment out the break.