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):

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