Velocity Script error - Change link by country | Community
Skip to main content
New Participant
June 6, 2017
Solved

Velocity Script error - Change link by country

  • June 6, 2017
  • 1 reply
  • 2478 views

I'm completely new to Velocity scripting, hoping it can solve a problem I'm having.

I want to serve up different links in an email based on what country is listed in a lead's record.  I do not want to use dynamic content/segmentation for this, as we'd use this link in lots of emails and I'd prefer not to have to set it up each time.  I took a script from another discussion and modified it as such:

#set( $linksBLC = { 

  "United Kingdom" : "website.com/page2", 

  "United States" : "website.com/page3", 

  "*" : "website.com/page1

}) 

#set( $link = $linksBLC[$lead.Country] ) 

#if ( !$link ) 

  #set( $link = $linksBLC['*'] ) 

#end 

<a href="$link">Click here</a>##

This gives me the following error:

Cannot get email content-

An error occurred when procesing the email Rendered_Email_Velocity_Error_Area_?!

near

?

Not the most helpful error, and turns up zero results in google.  Could someone let me know what I'm doing wrong?

The list would eventually contain 60+ countries, just trying to get it working before I scale it.

Thanks!

Best answer by SanfordWhiteman

Looks like you aren't trapping a nonexistent key. Try this:

#set( $linksBLC = {

  "United Kingdom" : "http://website.com/page2",

  "United States" : "http://website.com/page3",

  "*" : "http://website.com/page1"

})

#if( $linksBLC.containsKey($lead.Country) )

#set( $link = $linksBLC[$lead.Country] )

#else

#set( $link = $linksBLC['*'] )

#end

<a href="$link">Click here</a>##

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
June 6, 2017

Looks like you aren't trapping a nonexistent key. Try this:

#set( $linksBLC = {

  "United Kingdom" : "http://website.com/page2",

  "United States" : "http://website.com/page3",

  "*" : "http://website.com/page1"

})

#if( $linksBLC.containsKey($lead.Country) )

#set( $link = $linksBLC[$lead.Country] )

#else

#set( $link = $linksBLC['*'] )

#end

<a href="$link">Click here</a>##

Ian_ShawAuthor
New Participant
June 7, 2017

Thanks for taking the time to look at it Sanford.

Your amend fixed the error I was getting, but output the link as $link when I tested with a live send.  I fixed that by removing the from the links and putting them in the html on line 11, as per your blog.  That left me with this, which seems to work fine:

#set( $linksBLC = { 

  "United Kingdom" : "site1.com/", 

  "United States" : "site2.com/", 

  "*" : "site3.com/" 

}) 

#if( $linksBLC.containsKey($lead.Country) )

#set( $link = $linksBLC[$lead.Country] ) 

#else

#set( $link = $linksBLC['*'] ) 

#end 

<a href="${link}">Click here</a>##

Cheers!

SanfordWhiteman
New Participant
June 7, 2017

Yep, looks good! You're right, I didn't follow my own advice on the link part. In my defense, I was on a cross-country bus at the time!