Velocity script on Custom Object

Kacper_Gawlik1
Level 1

Velocity script on Custom Object

Hello!

I was looking for this within the forum but could not find ideal solution for my issue.

I am using following script on pulling out particualr content however, I wanted to show default content when contact is not in my CO

#foreach($content in $nurture_COList)
#set($url = $content.url)
#set($urlLength = $url.length() - 0) ## gets the length of the url
#set($trimUrl = $url.substring(8,$urlLength)) ## takes the sub-string starting after https://
<p><strong><a href="https://${trimUrl}">${content.title}</a></strong><br />
</p>
#end‍‍‍‍‍‍‍‍‍‍‍‍‍‍

$content is a field that is pushed by the external provider that pushed data into it.

There is also MarketoId field to which I want to refer when it's empty then load default content i.e.

#foreach($content in $nurture_COList)
#set($url = $content.url)
#set($urlLength = $url.length() - 0) ## gets the length of the url
#set($trimUrl = $url.substring(8,$urlLength)) ## takes the sub-string starting after https://
<p><strong><a href="https://${trimUrl}">${content.title}</a></strong><br />
</p>
#if $(nurture_COList.get(0).marketoId.isEmpty())
My Static Value
#end
#end

But that does not seem to work.

Anyone is able to help me withi this? Appreciate any insight!

Thanks,
Kacper

7 REPLIES 7
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script on Custom Object

Right now, you're checking if the first record in the CO has a certain value.

You want to check if the current record has that value. Otherwise you're checking the same thing on every turn of the loop. Doesn't make sense.

Also, String.substring is overloaded. You can pass only the startIndex. You don't need the endIndex.

#foreach( $content in $nurture_COList )
#set( $url = $content.url )
## takes the sub-string starting after https://
#set( $trimUrl = $url.substring(8) )
<p><strong><a href="https://${trimUrl}">${content.title}</a></strong><br /></p>
#if( $content.marketoId.isEmpty() )
My Static Value
#end
#end‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Kacper_Gawlik1
Level 1

Re: Velocity script on Custom Object

Hi Sanford,

What I actually want to check is if contact is on CO to show the content in <p></p> and if not, show static value.

${idio_Nurture_ArticleList.get(0).marketoId}
#foreach($content in $nurture_COList)
#set($url = $content.url)
#set($urlLength = $url.length() - 0) ## gets the length of the url
#set($trimUrl = $url.substring(8,$urlLength)) ## takes the sub-string starting after https://
<p><strong><a href="https://${trimUrl}">${content.title}</a></strong><br />
</p>
#if $(nurture_COList.get(0).marketoId.isEmpty())
My Static Value
#end
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script on Custom Object

That's broken code -- and you changed the nesting.

Can you please describe your requirements without using code?

Kacper_Gawlik1
Level 1

Re: Velocity script on Custom Object

If contact is associated with CO, content is displayed based on line 6,7 from previous post.

I would like to show default content whenever contact is not associated with CO.

Thanks,

Kacper

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script on Custom Object

It's a basic #if/#else.  You already have the ingredients.

#if( !$content.marketoId.isEmpty() )
My Dynamic Value
#else
My Static Value
#end
Kacper_Gawlik1
Level 1

Re: Velocity script on Custom Object

Thanks Sanford!

Ok I did not clarify it all.

In general $content is something that is loaded by external provider into CO when contact is associated. This field is not on list of fields loaded in CO while setting up:
pastedImage_1.png

So we are checking whether $content is in CO. So I rather looking for a scriptc that checks if that field is pushed to CO, if not then load static content: i.e. but this doesn't work:

#if(!$content in $nurture_COList)
My Static Value
#end

And for example below works but in both ways (if contact is in CO and is not) My Dnamic Value is loaded, I double checked contact association.

#if(!$nurture_CO.marketoId.isEmpty() )
My Dynamic Value
#else
My Static Value
#end‍‍‍‍‍

Are you able to advise?

Thanks,
Kacper

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script on Custom Object

This field is not on list of fields loaded in CO while setting up

Then it will not be present in Velocity. Only fields checked off in the tree are exposed in the Velocity context.