I need the output of a velocity script to include just the URL (and non of the URL parameters that follow the ? (stripping out the ? as well). Can you velocity gurus assist? I'm thinking I need to use "TruncateAtWord".
Solved! Go to Solution.
LinkTool is what you should use for URL manipulation (don’t try to parse URLs yourself).
#set( $url = $link.uri($lead.urlField) )
#set( $void = $url.setQuery($display.alt()) )
#set( $void = $url.setFragment($display.alt()) )
${url}
Should do that check before parsing the URL.
#if( !$lead.lastFormURL.isEmpty() )
#set( $url = $link.uri($lead.lastFormURL) )
#set( $void = $url.setQuery($display.alt()) )
#set( $void = $url.setFragment($display.alt()) )
#set( $final = ${url} )
#else
#set( $final = "Not Available" )
#end
${final}
LinkTool is what you should use for URL manipulation (don’t try to parse URLs yourself).
#set( $url = $link.uri($lead.urlField) )
#set( $void = $url.setQuery($display.alt()) )
#set( $void = $url.setFragment($display.alt()) )
${url}
Thanks Sandy. Can you explain further using this example?:
I just want to return this: https://www.avanade.com/en/services/microsoft-tech/modern-workplace/copilot-offering
Well, if you had a Person field called URL Field ($lead.urlField
in my code) set to the first URL, the output would be the second URL. 😉
It works by
null
— I assumed you wanted to strip the hash too, if it existsThe URL is stored in a custom field: "LastURL".
I still don't see where I would truncate the ? - and everything after it - using the code you provided.
? The code is truncating the final URL. That’s what happens when you null out the query string property before outputting the URL.
It’s like doing document.location.search = "";
in JavaScript.
Now I see. Thanks so much, Sandy - this worked perfectly! I also included additional code to return "Not Available" if the last URL does not exists:
#set( $url = $link.uri($lead.lastFormURL) )
#set( $void = $url.setQuery($display.alt()) )
#set( $void = $url.setFragment($display.alt()) )
#if( !$lead.lastFormURL.isEmpty() )
#set( $final = ${url} )
#else
#set( $final = "Not Available" )
#end
${final}
Should do that check before parsing the URL.
#if( !$lead.lastFormURL.isEmpty() )
#set( $url = $link.uri($lead.lastFormURL) )
#set( $void = $url.setQuery($display.alt()) )
#set( $void = $url.setFragment($display.alt()) )
#set( $final = ${url} )
#else
#set( $final = "Not Available" )
#end
${final}