Re: Velocity Script Custom Object urls - foreach Loop URLs mixed in output

Kacper
Level 1

Velocity Script Custom Object urls - foreach Loop URLs mixed in output

Hi Everyone,

Looking for a guidance on a Velocity script solution that I'm building currently.
I need to populate content pushed to CO in a following format (deduped by personId, category):

Kacper_0-1687270803964.png

So, I'm using below code to populate the content, but also trim each url from https:// so it can be later applied statically to enable tracking.
Each set of content has 3 urls placed in separate fields as highlighted in red.
I need to generate relevant content per category (in yellow).
It works great in Preview Email mode but, when I sent it from test campaigns (my record has not duplicates), what happens is, that for i.e. the url for Article 3 category oil is mixed with Article 3 category electric power.
Is there anything I can proof better in my logic or maybe try a different approach?

 

#foreach( $item in $blueConicRecommendations_cList )
#set( $url1 = $item.rec1_link)
#set( $url2 = $item.rec2_link)
#set( $url3 = $item.rec3_link)
#set($urlLength1 = $url1.length() - 0)## gets the length of the url
#set($urlLength2 = $url2.length() - 0)
#set($urlLength3 = $url3.length() - 0)
#set($trimUrl1 = $url1.substring(8,$urlLength1))
#set($trimUrl2 = $url2.substring(8,$urlLength2))
#set($trimUrl3 = $url3.substring(8,$urlLength3))


#if($item.category.equals("oil") )
<p style="font-size:16px;" class="one">› 
       	${item.rec1_description}
        <a href="https://${trimUrl1}">${item.rec1_title}</a><br /> 
        </p> 
         <p style="font-size:16px;" class="one">› 
       ${item.rec2_description}
        <a href="https://${trimUrl2}">${item.rec2_title}</a><br /> 
        </p>
        
        <p style="font-size:16px;" class="one">› 
       	${item.rec3_description}
        <a href="https://${trimUrl3}">${item.rec3_title}</a><br /> 
        </p>   
#end      
#if($item.category.equals("electric power") )

<p style="font-size:16px;" class="one">› 
       	${item.rec1_description}
        <a href="https://${trimUrl1}">${item.rec1_title}</a><br /> 
        </p> 
        
         <p style="font-size:16px;" class="one">› 
       ${item.rec2_description}
        <a href="https://${trimUrl2}">${item.rec2_title}</a><br /> 
        </p>
        
        <p style="font-size:16px;" class="one">› 
       	${item.rec3_description}
        <a href="https://${trimUrl3}">${item.rec3_title}</a><br /> 
        </p>
#end
#end

 

 

2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script Custom Object urls - foreach Loop URLs mixed in output

Definitely not the best way to remove the protocol. Use the regex from my article Creating trackable links even when a token includes “http://” or “https://”.

 

But that still won’t solve the problem, which is that you’re trying to reuse the same variable in multiple tracked links (it may not feel like it, but that’s what you’re doing when you iterate the list). So search for “multiple tracked links in Velocity” and you’ll see my article on that.

Kacper
Level 1

Re: Velocity Script Custom Object urls - foreach Loop URLs mixed in output

Thanks Sanford for picking this up!
I used Your method for protocol removal and I'm trying to apply the second functionality for the 3 links within category.

This is my initial set-up:

 

 

#foreach( $item in $blueConicRecommendations_cList )
#set( $url1 = $item.rec1_link)
#set( $url2 = $item.rec2_link)
#set( $url3 = $item.rec3_link)
#set( $url1NoSub = $url1.replaceAll("(?i)^https?://","") )
#set( $url2NoSub = $url2.replaceAll("(?i)^https?://","") )
#set( $url3NoSub = $url3.replaceAll("(?i)^https?://","") )

#evaluate( "${esc.h}set( ${esc.d}url1NoSub_${foreach.count} = ${esc.d}url1NoSub )" ) 
#evaluate( "${esc.h}set( ${esc.d}url2NoSub_${foreach.count} = ${esc.d}url2NoSub )" ) 
#evaluate( "${esc.h}set( ${esc.d}url3NoSub_${foreach.count} = ${esc.d}url3NoSub )" ) 

 

 

I was not quite sure how to apply item in the end of the element, so the below part, that's why I kept item in front for each referred url:

 

 

#foreach( $key in $lead.downloadcenter.split(";") )
  #set( $downloadLink = $allDownloadLinks[$key] )
  #evaluate( "${esc.h}set( ${esc.d}downloadLink_${foreach.count} = ${esc.d}downloadLink )" )

 

 



Next, I set-up an additional logic to place the right url with respect to the category, as per You article suggestion:

 

 

#if($item.category.equals("oil") && $url3NoSub_1)       
        <p style="font-size:16px;" class="one">› 
       	
        <a class="mktNoTrack" href="https://${url3NoSub_1}">${item.rec3_title}</a><br /> 
        ${url3NoSub_1}<br /> 
		${item.rec3_title}
        </p>   
#end      

#if($item.category.equals("electric power") && $url1NoSub_1)
<p style="font-size:16px;" class="one">› 
       
        <a class="mktNoTrack" href="https://${url1NoSub_1}">${item.rec1_title}</a><br /> 
        ${url1NoSub_1}<br /> 
		${item.rec1_title}
        </p> 
        
#end
#if($item.category.equals("electric power") && $url2NoSub_1)
 <p style="font-size:16px;" class="one">› 
       
        <a class="mktNoTrack" href="https://${url2NoSub_1}">${item.rec2_title}</a><br /> 
        ${url2NoSub_1}<br /> 
		${item.rec2_title}
        </p>
#end
#if($item.category.equals("electric power") && $url3NoSub_1)       
        <p style="font-size:16px;" class="one">› 
       
        <a class="mktNoTrack" href="https://${url3NoSub_1}">${item.rec3_title}</a><br /> 
        ${url3NoSub_1}<br /> 
		${item.rec3_title}
        </p>   
#end    
#end

 

 

 and to some point it works. I generated the preview with separate section (oil or electric power) for urls per loop:

 

 

<b>START LOOP URLS Only no. ${foreach.count}</b><br>
url1 ${foreach.count} <br>
#evaluate($url1NoSub_1)<br>
url2 ${foreach.count}<br>
#evaluate($url2NoSub_1)<br>
url3 ${foreach.count}<br>
#evaluate($url3NoSub_1)<br>
<b>END LOOP URLS Only no. ${foreach.count}</b><br>

 

 


I put urls outside to make sure if it loads correctly per example:

Kacper_0-1687350678014.png

 

And it works quite well in Preview and technically when I sent the email it looks ok too, for the first category (color-coded are the expected set of urls), but when I click the url title(highlighted in red) it pushes the url from 2nd category (highlighted in red on another screen):

Kacper_3-1687351734670.png

Kacper_4-1687351756603.png

 

Should I be doing something differently?