SOLVED

Email Script Token breaking hyperlink

Go to solution
Taylor_McCarty
Level 4

Email Script Token breaking hyperlink

So I have a scripting token that is pulling a unique URL off the Opportunity object and I want to then use that URL for the hyperlink on the button in the email. However something is breaking the HTML code from the button and ends up displaying the URL and then the broken code for the button. Below is a screenshot of what it is doing, I had to block out the URL because it's a unique link related to each opportunity. 

Screen Shot 2019-08-08 at 1.37.28 PM.png

2 ACCEPTED SOLUTIONS

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token breaking hyperlink

Your code should be refactored more like so:

#set( $interestingCompanyName = "CompanyCo, Inc." )
#set( $defaultLink = "www.example.com/generic-landing-page" )
#set( $EXC_LINK_NO_LINK = "No E-Comm Link" )
#foreach( $o in $sorter.sort( $OpportunityList, "MarketoCreatedAt:desc") )
#if( $o.Company_Name__c.equals($interestingCompanyName) )
#if( !$o.E_Comm_Link__c.equals($EXC_LINK_NO_LINK) && $lead.Number_of_Opportunities.equals(1) )
#set( $outputLink = $o.E_Comm_Link__c )
#end
#break
#end
#end
#set( $outputLink = $display.alt($outputLink, $defaultLink) )
<a href="https://${outputLink}">Click Me</a>

But still, the relevant change is that the entire A is output.

Naturally I'm assuming you don't have any typos in field names and all the fields are checked off in the tree.

View solution in original post

Taylor_McCarty
Level 4

Re: Email Script Token breaking hyperlink

Thanks for helping with this. And I think I am understanding what your code is doing, but again having to just learn this on my own with no coding background has been interesting and challenging. 

However I was able to figure out the issue, and though my code wasn't optimal, it wasn't the code. For some reason, the value that SFDC was giving me for this field was already a hyperlink (I am meeting with the SFDC team to understand why this was a hyperlink and not just a URL like I was expecting it to be). So what was happening is I was trying to use a hyperlink inside a hyperlink.

So the HTML on my email ends up looking like this:

<a href="<a href="https://www.unique-link.com">Click Me</a>">Click Me</a>

Which of course breaks things and results in the images in my original post.

Again thanks though for your help! 

View solution in original post

8 REPLIES 8
SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token breaking hyperlink

You haven't supplied enough information to troubleshoot -- there isn't even any code in your post!

When using Velocity, you must output the entire link -- from the opening <a> to closing </a> -- in Velocity.

Taylor_McCarty
Level 4

Re: Email Script Token breaking hyperlink

I'm reluctant to output the code because it has some other sensitive information that I am not sure our legal team would allow to be displayed.

However I am not making the link in the velocity script. I am just asking that it return the URL. Then I am just adding the token into the hyperlink, so my email looks like this:

<a href="{{my.OPP_Ecomm_Link}}">Link</a>

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token breaking hyperlink

You need to follow the standard links-in-Velocity  guideline I noted. 

Taylor_McCarty
Level 4

Re: Email Script Token breaking hyperlink

Not sure if this will help, but here is my code, just edited a little so that its hopefully not showing anything that my legal team would get me in trouble for displaying. I am sure this code isn't very sophisticated and could be written way better, but this is the best I got from teaching myself this stuff and reusing code from previously build tokens from someone else that is no longer at the company.

#foreach( $o in $sorter.sort(${OpportunityList}, "MarketoCreatedAt:desc") )
#if( $oppFound == 0 && $o.Company_Name__c && $o.Company_Name__c == "Name of Company" )
#set ( $oppFound = 1 )
#if( $o.E_Comm_Link__c == "No E-Comm Link")
https://www.generic-landing-page.com##
## <br>No E-Comm Link##
#elseif( ${lead.Number_of_Opportunities} == 1)
## #set( $b = $o.E_Comm_Link__c.replace("https://", "") )
## $b <br>##
## <a href="$o.E_Comm_Link__c">Click me</a><br>##
$o.E_Comm_Link__c##
## <br>Has E-comm link##
#else
https://www.generic-landing-page.com##
## <br>Greater Than One Opp##
#end
#end
#end
#if ( $oppFound == 0 )
https://www.generic-landing-page.com##
#end
SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token breaking hyperlink

Certainly not the optimal code (I can't rewrite it right now as I'm in a car) but the problem, again, is you aren't following the requirements. You have to output the entire HTML A element. Not just the href (URL). The whole A.

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token breaking hyperlink

Your code should be refactored more like so:

#set( $interestingCompanyName = "CompanyCo, Inc." )
#set( $defaultLink = "www.example.com/generic-landing-page" )
#set( $EXC_LINK_NO_LINK = "No E-Comm Link" )
#foreach( $o in $sorter.sort( $OpportunityList, "MarketoCreatedAt:desc") )
#if( $o.Company_Name__c.equals($interestingCompanyName) )
#if( !$o.E_Comm_Link__c.equals($EXC_LINK_NO_LINK) && $lead.Number_of_Opportunities.equals(1) )
#set( $outputLink = $o.E_Comm_Link__c )
#end
#break
#end
#end
#set( $outputLink = $display.alt($outputLink, $defaultLink) )
<a href="https://${outputLink}">Click Me</a>

But still, the relevant change is that the entire A is output.

Naturally I'm assuming you don't have any typos in field names and all the fields are checked off in the tree.

Taylor_McCarty
Level 4

Re: Email Script Token breaking hyperlink

Thanks for helping with this. And I think I am understanding what your code is doing, but again having to just learn this on my own with no coding background has been interesting and challenging. 

However I was able to figure out the issue, and though my code wasn't optimal, it wasn't the code. For some reason, the value that SFDC was giving me for this field was already a hyperlink (I am meeting with the SFDC team to understand why this was a hyperlink and not just a URL like I was expecting it to be). So what was happening is I was trying to use a hyperlink inside a hyperlink.

So the HTML on my email ends up looking like this:

<a href="<a href="https://www.unique-link.com">Click Me</a>">Click Me</a>

Which of course breaks things and results in the images in my original post.

Again thanks though for your help! 

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token breaking hyperlink

Errors in your back-end data aside, you must emit A links entirely from Velocity for them to work reliably.

Luckily, this data was already an entire A, so you will now be doing it right.

Also note that multiple links within the same token require special treatment, as I've written about extensively.