SOLVED

Velocity Script, foreach loop, trying to display multiple results.

Go to solution
Level 2

Velocity Script, foreach loop, trying to display multiple results.

Hello Community

 

We are trying to come up with Velocity Script to display field data from an Custom Object when certain criteria are met. I have it working when there is just one qualifying product but when a person has 2 or more qualifying products, the script does not complete and when I preview the email it give a Communication Failure error.

 

I have studied many of the really helpful Velocity posts / blogs on this board and have tried to follow the solutions - I suspect I have missed something pretty fundamental somewhere.

 

To explain what the below is trying to acheive...

1. Create a list of products that qualify 

2. Look at the smaller list and display results

 

I appreciate any help given !

 

 

## Find qualifying products & create list
#foreach( $Product in $Object1__cList )
#set( $qualifyingProducts = [] )
#if( $Object1.Days_to_Expire__c.equals("30") )
#set( $void = $qualifyingProducts.add($Product) )
#end
#end

## Search list 
#foreach( $Product in $qualifyingProducts )
#set($startDate = $convert.parseDate(${Object1.Warranty_Start__c}, "yyyy-MM-dd"))
#set($formattedStartDate = $date.format("dd/MM/yyyy", $startDate))
#set($endDate = $convert.parseDate(${Object1.Warranty_End__c}, "yyyy-MM-dd"))
#set($formattedEndDate = $date.format("dd/MM/yyyy", $endDate))

#set($defaultTimeZone = $date.getTimeZone().getTimeZone("Europe/London") )
#set($defaultLocale = $date.getLocale() )
#set($calNow = $date.getCalendar() )
#set($ret = $calNow.setTimeZone($defaultTimeZone) )
#set($calConst = $field.in($calNow) )

## Display results in a table, if there are multiple results display more than just one.
Your Product Warranty is about to expire...
Product Name: $Object1.Product_Name__c
Warranty End Date: $formattedEndDate
</br>

#end

 

*******

I need to add (I think) an important point... also included in the results that I am displaying in the table is a link !
I understand this may cause issues... https://blog.teknkl.com/multiple-marketo-tracked-links-in-velocity/

The link is inside a #if / #else line, the link includes a field... e.g. href="https://xxx.xxx/xxx/xx?pk=${Object1.field}

Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
Level 10 - Community Moderator

Re: Velocity Script, foreach loop, trying to display multiple results.

More than one fatal problem here.

  1. $Object1 is null; at no point do you declare that variable, rather you're using $Product as your iterator variable.
  2. You keep resetting the filtered list to the empty list []. This needs to be declared once, outside the loop.

Non-fatal but still inadvisable:

  1. Declare your calendar constants once.
  2. Don't use curly braces inside #methods.

It should be more like this:

## Declare date math constants
#set($defaultTimeZone = $date.getTimeZone().getTimeZone("Europe/London") )
#set($defaultLocale = $date.getLocale() )
#set($calNow = $date.getCalendar() )
#set($ret = $calNow.setTimeZone($defaultTimeZone) )
#set($calConst = $field.in($calNow) )
## Filter qualifying products into separate list
#set( $qualifyingProducts = [] )
#foreach( $Product in $Object1__cList )
#if( $Product.Days_to_Expire__c.equals("30") )
#set( $void = $qualifyingProducts.add($Product) )
#end
#end
## Display list items
#foreach( $Product in $qualifyingProducts )
#set($startDate = $convert.parseDate($Product.Warranty_Start__c, "yyyy-MM-dd"))
#set($formattedStartDate = $date.format("dd/MM/yyyy", $startDate))
#set($endDate = $convert.parseDate($Product.Warranty_End__c, "yyyy-MM-dd"))
#set($formattedEndDate = $date.format("dd/MM/yyyy", $endDate))
Your Product Warranty is about to expire...
Product Name: ${Product.Product_Name__c}
Warranty End Date: ${formattedEndDate}
</br>
#end

 

You're correct that you'll have to use the techniques from that blog post to handle links within the loop. Either that or deliberately make them untracked (class="mktNoTrack").

View solution in original post

1 REPLY 1
Level 10 - Community Moderator

Re: Velocity Script, foreach loop, trying to display multiple results.

More than one fatal problem here.

  1. $Object1 is null; at no point do you declare that variable, rather you're using $Product as your iterator variable.
  2. You keep resetting the filtered list to the empty list []. This needs to be declared once, outside the loop.

Non-fatal but still inadvisable:

  1. Declare your calendar constants once.
  2. Don't use curly braces inside #methods.

It should be more like this:

## Declare date math constants
#set($defaultTimeZone = $date.getTimeZone().getTimeZone("Europe/London") )
#set($defaultLocale = $date.getLocale() )
#set($calNow = $date.getCalendar() )
#set($ret = $calNow.setTimeZone($defaultTimeZone) )
#set($calConst = $field.in($calNow) )
## Filter qualifying products into separate list
#set( $qualifyingProducts = [] )
#foreach( $Product in $Object1__cList )
#if( $Product.Days_to_Expire__c.equals("30") )
#set( $void = $qualifyingProducts.add($Product) )
#end
#end
## Display list items
#foreach( $Product in $qualifyingProducts )
#set($startDate = $convert.parseDate($Product.Warranty_Start__c, "yyyy-MM-dd"))
#set($formattedStartDate = $date.format("dd/MM/yyyy", $startDate))
#set($endDate = $convert.parseDate($Product.Warranty_End__c, "yyyy-MM-dd"))
#set($formattedEndDate = $date.format("dd/MM/yyyy", $endDate))
Your Product Warranty is about to expire...
Product Name: ${Product.Product_Name__c}
Warranty End Date: ${formattedEndDate}
</br>
#end

 

You're correct that you'll have to use the techniques from that blog post to handle links within the loop. Either that or deliberately make them untracked (class="mktNoTrack").

View solution in original post