SOLVED

Re: Velocity Script error - Change link by country

Go to solution
Ian_Shaw
Level 2

Velocity Script error - Change link by country

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" : "http://website.com/page2", 

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

  "*" : "http://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!

Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script error - Change link by country

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>##

View solution in original post

3 REPLIES 3
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script error - Change link by country

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_Shaw
Level 2

Re: Velocity Script error - Change link by country

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 http:// 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="http://${link}">Click here</a>##

Cheers!

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script error - Change link by country

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!