SOLVED

Velocity Dynamic Content Sorting

Go to solution
Jayant_Singh
Level 3

Velocity Dynamic Content Sorting

I have imported a list of records to custom objects which contains data based on user visits in the website. By using this list trying to create email in Marketo. From CO list, selecting only recent visit of the product. In the given list, have product name, image, URL and recent visit date.

 

Problem- I have created three velocity script and sorting the list based on recent visit date. But, in email output is display incorrect.

 

Jayant_Singh_0-1590074462697.png

 

Below three velocity script used for sorting the product name, image and URL.

 

 

 

Script-1

<html>
<body>
<table>
## start product image select
<tbody>
#set( $DEFAULT_Product = "No item in list." )
<tr>
#if( $pro_cList.isEmpty() )
#set( $outputItemimage = $DEFAULT_Product )
#else
#foreach( $pro_c in $sorter.sort($pro_cList, "date:desc") )
#if( !$display.alt($pro_cList["productImage"],"").isEmpty() )
#set( $outputItemimage = $pro_c["productImage"] )
#break
#end
#end
#set( $outputItemimage = $display.alt($outputItemimage, $DEFAULT_Product) )
#end
<td>
<img width="189" align="right" src="${outputItemimage}" border="0"/>
</td>
</tr>
</tbody>
</table>
</body>
</html>

 

 

 

 

 

Script-2

<html>
<body>
<table>
## start product name select
<tbody>
#set( $DEFAULT_Product = "No item in list." )
<tr>
#if( $pro_cList.isEmpty() )
#set( $outputName = $DEFAULT_Product )
#else
#foreach( $pro_c in $sorter.sort($pro_cList, "date:desc") )
#if( !$display.alt($pro_cList["productName"],"").isEmpty() )
#set( $outputName = $pro_c["productName"] )
#break
#end
#end
#set( $outputName = $display.alt($outputName, $DEFAULT_Product) )
#end
<td>
${outputName}
</td>
</tr>
</tbody>
</table>
</body>
</html>

 

 

 

 

 

Script-3

<html>
<body>
<table>
## start product URL select
<tbody>
#set( $DEFAULT_Product = "No item in list." )
<tr>
#if( $pro_cList.isEmpty() )
#set( $outputItemCTA = $DEFAULT_Product )
#else
#foreach( $pro_c in $sorter.sort($pro_cList, "date:desc") )
#if( !$display.alt($pro_cList["productCTA"],"").isEmpty() )
#set( $outputItemCTA = $pro_c["productCTA"] )
#break
#end
#end
#set( $outputItemCTA = $display.alt($outputItemCTA, $DEFAULT_Product) )
#end
<td>
<a href="${outputItemCTA}">Check out</a>
</td>
</tr>
</tbody>
</table>
</body>
</html>

 

 

 

Jayant
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Dynamic Content Sorting

This line:

#if( !$display.alt($pro_cList["productName"],"").isEmpty() )

 

Should be:

#if( !$display.alt($pro_c["productName"],"").isEmpty() )

 

Remember, when you're iterating over the items ($pro_c) in the list, you want to test the item, not the whole list.

View solution in original post

20 REPLIES 20
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Dynamic Content Sorting

Hi Jayant,

 

This seems like an overly wordy (and thus fragile) way to do this — it could easily be a single token with a #macro since the logic is the same other than the field name.

 

Beyond that, I'd need to see a dump of all of the data from the list. Chances are, for the fields that are outputting the default value, you don't have them checked off in the tree in Script Editor. 

Jayant_Singh
Level 3

Re: Velocity Dynamic Content Sorting

Hi San,

Before creating macro, I want to fetch the data for each field correctly. So, I am not able to find checked off condition for the script. Can you please suggest more in this?

Jayant_Singh_0-1590158250005.png

 

 

Jayant
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Dynamic Content Sorting

Not sure I understand what you're saying here.  When I say "checked off" I mean in the tree (of Lead and Object fields) on the right-hand-side of Script Editor.

Jayant_Singh
Level 3

Re: Velocity Dynamic Content Sorting

I have already selected that one but not sure why it is evaluating only default output this is the worry part and I am not starting macro unless I am sure that I can get output individually correct. 

Jayant
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Dynamic Content Sorting

Please show a screenshot of the Script Editor tree with the checkboxes checked for all the fields.

 

If you want to check to see if something has a non-null value (including an empty String, which is a value) just output that ${lead.field}.  If you've failed to check off the field, you'll see the literal VTL code, since that's what Velocity prints if the value is null.

Jayant_Singh
Level 3

Re: Velocity Dynamic Content Sorting

Jayant_Singh_0-1590174159415.pngJayant_Singh_0-1590174480876.pngJayant_Singh_0-1590174238138.png

I am selecting CO list tree. am I missing anything?

 

Jayant
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Dynamic Content Sorting

I see 3 fields, sure, though what their Velocity property names are isn't clear.

 

Run this standard CO debugging code:

 

#set( $coList = $pro_cList )
#foreach( $record in $coList )
#foreach( $kv in $record.entrySet() )
${kv.getKey()} ${display.alt($kv.getValue().class,"")} ${display.alt($kv.getValue(),"null")}
#end
#end

 

Jayant_Singh
Level 3

Re: Velocity Dynamic Content Sorting

In the given code,

1. kv and record will be replaced with any property?

2. This code need to add in velocity script in starting

 

Jayant
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Dynamic Content Sorting

Nope, nothing to replace. 

 

Just put this exact at the top of your token to get debug output.