Hello!
Sorry, back again with more fun weird use cases. I'm looking to see if it is possible to have commas between outputs from a single field if there is more than one result? Here is my current script that looks for a opening date in the last 5 days and prints the Full Name:
##Standard Velocity Date/Time Fields
#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/Chicago") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ss" )
#set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" )
#set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" )
## iterate Customer Profiles, looking for those with Membership Opening yesterday
#foreach ( $item in $customerProfilesList )
#set( $membershipOpeningDate = $convert.parseDate(
$item.membershipDate,
$ISO8601DateOnly,
$defaultLocale,
$defaultTimeZone
) )
##Looks to see if Membership date is in last 5 days, due to data upload
#if ( ($item.membershipStatus.equals("Member")) && ( ($date.difference($calNow,$membershipOpeningDate).getDays().intValue().equals(-1)) || ($date.difference($calNow,$membershipOpeningDate).getDays().intValue().equals(-2)) || ($date.difference($calNow,$membershipOpeningDate).getDays().intValue().equals(-3)) || ($date.difference($calNow,$membershipOpeningDate).getDays().intValue().equals(-4)) || ($date.difference($calNow,$membershipOpeningDate).getDays().intValue().equals(-5)) ) )
$item.fullName##
#else
#end
#end
I looked into using Sanford's solution here: Marketo tokens - commas and spaces when using seve... - Marketing Nation but that was looking at output from several fields potentially, versus one field - so I was not sure if the concept would be the same?
Solved! Go to Solution.
Inside the loop, you’re continually setting $fullNames to an empty list and then populating it with a single item.
I don’t understand the question — where do commas come into play in your code/data?
But I will say please, please don't do the multiple comparisons that way. It’s very hard to read and also makes Marketo work harder for no reason.
Set the (Integer) difference once. Then there are 2 ways to compare.
Option 1: Integer.compareTo:
#set( $minDiff = -5 )
#set( $maxDiff = -1 )
#set( $diffDays = $date.difference($calNow,$membershipOpeningDate).getDays().intValue() )
#if( ( $diffDays.compareTo($minDiff).equals(1) || $diffDays.equals($minDiff) )
&& ( $diffDays.compareTo($maxDiff).equals(-1) || $diffDays.equals($maxDiff) ) )
between min and max, inclusive
#end
Option 2: Collection.contains:
#set( $minDiff = -5 )
#set( $maxDiff = -1 )
#set( $diffDays = $date.difference($calNow,$membershipOpeningDate).getDays().intValue() )
#set( $acceptableDiffs = [$minDiff..$maxDiff] )
#if( $acceptableDiffs.contains($diffDays) )
between min and max, inclusive
#end
Sorry, let me try to clarify: Within a custom object, if there's a case of multiple records meeting the criteria like:
Lead Email | Membership Opening Date | First Name |
bob@gmail.com | 2023-03-29 | Bob |
bob@gmail.com | 2023-03-29 | Susan |
bob@gmail.com | 2023-03-29 | Joe |
We would potentially just send one email instead of three. By default, printing a First Name field would output like:
"Welcome Bob Susan Joe!" and what I'd like is for it to print out like "Welcome Bob, Susan, and Joe".
Basically, a case of if there is more than one output that meets the criteria have it look nicer.
Hopefully that helps?
I tried using the below, and the output is still without the comma delimiters in-between:
$display.list($item.fullName)
Even trying to do a basic token test like:
#foreach ($item in $customerProfilesList)
#set ($list = $item.fullName)
$display.list($list)
#end
It still prints as "Name 1 Name 2 Nam2 3" instead of how it looks like it should function.
You have to pass it a collection or list! $item.fullName is a string that’s set every time the loop turns. It’s not a list.
You can add all the $fullName property values to a list:
#set( $fullNames = [] )
#foreach( $item in $customerProfilesList )
#set( $void = $fullNames.add($item.fullName) )
#end
${display.list($fullNames)}
Or use the 4th overload to extract a single property in one call:
${display.list($customerProfilesList, ", ", " and ", "fullName")}
Thank you! Is there a best way to incorporate this into the larger script action?
I'm basically trying to get the output here:
${display.list($fullNames)}
to follow my #if condition and it seems like if I run that #foreach loop before my membershipdate one and then put the output after the if statement, it outputs twice, once correctly. Like this:
##Standard Velocity Date/Time Fields
#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/Chicago") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ss" )
#set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" )
#set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" )
#set( $fullNames = [] )
#foreach( $item in $customerProfilesList )
#set( $void = $fullNames.add($item.fullName) )
#end
## iterate Customer Profiles, looking for those with Membership Opening yesterday
#foreach( $item in $customerProfilesList )
#set( $membershipOpeningDate = $convert.parseDate(
$item.membershipDate,
$ISO8601DateOnly,
$defaultLocale,
$defaultTimeZone
) )
##Sets difference from Membership Opening Date between 1 and 7 days
#set( $minDiff = -7 )
#set( $maxDiff = -1 )
#set( $diffDays = $date.difference($calNow,$membershipOpeningDate).getDays().intValue() )
#if( ( $diffDays.compareTo($minDiff).equals(1) || $diffDays.equals($minDiff) )
&& ( $diffDays.compareTo($maxDiff).equals(-1) || $diffDays.equals($maxDiff) ) )
${display.list($fullNames)}
#else
#end
#end
Which I know isn't correct - but I guess I don't know if there is a preferred order of operations?
You’re outputting the whole list every time the condition (date diff range) matches. So that’s not just 2 times, it’s an unlimited number of times depending on how many records there are that match the condition.
Obviously if you only want it to output once, it needs to be outside the #foreach loop.
I got it to work in theory, as setting it outside the #foreach loop will display correctly - as in it takes the full name of all records, but then it ignores my #if conditional statement.
##Standard Velocity Date/Time Fields
#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/Chicago") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ss" )
#set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" )
#set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" )
#set( $fullNames = [] )
#foreach( $item in $customerProfilesList )
#set( $void = $fullNames.add($item.fullName) )
#end
## iterate Customer Profiles, looking for those with Membership Opening yesterday
#foreach( $item in $customerProfilesList )
#set( $membershipOpeningDate = $convert.parseDate(
$item.membershipDate,
$ISO8601DateOnly,
$defaultLocale,
$defaultTimeZone
) )
##Sets difference from Membership Opening Date between 1 and 7 days
#set( $minDiff = -7 )
#set( $maxDiff = -1 )
#set( $diffDays = $date.difference($calNow,$membershipOpeningDate).getDays().intValue() )
#if( ( $diffDays.compareTo($minDiff).equals(1) || $diffDays.equals($minDiff) )
&& ( $diffDays.compareTo($maxDiff).equals(-1) || $diffDays.equals($maxDiff) ) )
#else
#end
#end
${display.list($fullNames)}
Do I need to put the loop for setting the list inside the conditional?
It’s not ignoring the condition — you’re making the decision to not conditionally add names to the list, instead adding every name before you even enter the loop + condition.
Again, think “What am asking Velocity to do?” Narrate your code out loud and you’ll see it’s just following orders.