SOLVED

Re: #foreach velocity question

Go to solution
Daniela_Puizina
Level 1

#foreach velocity question

Hi all,

I'm trying to create a script that says if a contact has an account owner of any of the stated emails (below), display html and if not display different html. However, when I try to test it, I get this error: 

Cannot get email content- <div>An error occurred when procesing the email Body! </div> <p>Encountered ") \n" near</p> <div><pre >#if( $accountowner.matches($email) ) </pre><pre > #set($foundMatch) </pre><pre class="x-form-item-label"> #break </pre><pre >#if($foundMatch)</pre><pre >correct html</pre></div>

One of our front-end engineers had helped me with it but he isn't familiar with velocity so I'm not sure if it's just a simple syntax error. Any ideas?

#set ($accountowner = "${lead.Account_Owner_Email_Address}") 
#set ($foundMatch = false)
#set( $outputByEmail = [
"test1@em.com",
"test2@em.com",
"test2@em.com",
"test4@em.com" ])
#foreach( $email in $outputByEmail )
#if( $accountowner.matches($email) )
#set($foundMatch)
#break
#if($foundMatch)
correct html

#else

defaulthtml

#end‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: #foreach velocity question

You do have fundamental syntax errors.

  1. You're missing 2 #end statements.
  2. #set( $foundMatch ) is not valid Velocity. #set( $foundMatch = true ) is valid.

However, the whole loop-and-break thing isn't necessary here. You're trying to find an exact match in a list. That's what List.contains is for.

#set( $accountOwnerEmail = $lead.Account_Owner_Email_Address ) 
#set( $outputByEmail = [
"test1@em.com",
"test2@em.com",
"test2@em.com",
"test4@em.com"
] )
#set( $foundMatch = $outputByEmail.contains($accountOwnerEmail) )
#if( $foundMatch )
correct html
#else
defaulthtml
#end‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Probably not a good idea to have a non-Velocity dev help you w/Velocity.   It requires specialized knowledge.

View solution in original post

3 REPLIES 3
SanfordWhiteman
Level 10 - Community Moderator

Re: #foreach velocity question

You do have fundamental syntax errors.

  1. You're missing 2 #end statements.
  2. #set( $foundMatch ) is not valid Velocity. #set( $foundMatch = true ) is valid.

However, the whole loop-and-break thing isn't necessary here. You're trying to find an exact match in a list. That's what List.contains is for.

#set( $accountOwnerEmail = $lead.Account_Owner_Email_Address ) 
#set( $outputByEmail = [
"test1@em.com",
"test2@em.com",
"test2@em.com",
"test4@em.com"
] )
#set( $foundMatch = $outputByEmail.contains($accountOwnerEmail) )
#if( $foundMatch )
correct html
#else
defaulthtml
#end‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Probably not a good idea to have a non-Velocity dev help you w/Velocity.   It requires specialized knowledge.

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: #foreach velocity question

Also: as I noted, you'd be better served by List.contains here, not matches. But matches was also being badly misused before.

String.matches assumes its argument is a regular expression pattern. Not a simple string.  So in your original code, you were matching patterns like

test@1em.com‍‍

The period in a pattern stands for "any character", not a literal period. So that pattern matches "test@1em.com" but also matches "test@1emAcom" and "test@1em1com".  So it wasn't put through enough testing.

Daniela_Puizina
Level 1

Re: #foreach velocity question

Thanks, Sanford! It works now

Contains vs matches makes sense, thank you for explaining. I showed the dev this - I think we will have an easier time in the future!