foreach in Velocity returns too much

Robb_Barrett
Marketo Employee

foreach in Velocity returns too much

I'm trying to create a simple script in Velocity which will use the ZIP code provided by a person who fills out a form to assign a rep.  Ideally, it will run the ZIP through several arrays of postal codes to find the correct rep to assign. Here's an example of what I've written:

#set( $robb = ["11111", "111", "111111"] )
#set( $notrobb = ["1111111111", "1"])
 
#foreach ($lead.PostalCode in $robb)
Robb 
#end
 
#foreach ($lead.PostalCode in $notrobb)
Not Robb 
#end


I put this in an alert email.  When I test, this is what I see in the email:

Rep Name:  Robb Robb Robb Not Robb Not Robb 

 
Robb Barrett
Tags (1)
2 REPLIES 2
Robb_Barrett
Marketo Employee

Re: foreach in Velocity returns too much

Monday bumping this.  Anyone know a good with to write a script for IF <attribute> IN <array>?  I know how to limit this to one row returned, but it'll give me both the true and false result.
Robb Barrett
Anonymous
Not applicable

Re: foreach in Velocity returns too much

Hey Robb,

Your #foreach statements are just going to execute 'N' times, once for each value in $robb or $notrobb, and print the corresponding text each time it executes.  #foreach statements don't check for a value in an array, they just iterate 'N' times, once for each value in the array.  On each iteration, a value is getting assigned to $lead.PostalCode that is probably overwriting (in the context of the script, not in your lead database) the value inherited from the lead record.

Within each #foreach statement, you need a conditional that says #if (condition) Robb #else Not Robb.

So, you really want to do something like:

#set( $robb = ["11111", "111", "111111"] )
 
#foreach ($repCode in $robb)
  #if ($repCode == $lead.PostalCode)
    Robb
  #else
    NotRobb
  #end
#end
 
 
Hope that helps.

-patrick