Commas In-Between Outputs from Single Field? | Community
Skip to main content
New Participant
March 29, 2023
Solved

Commas In-Between Outputs from Single Field?

  • March 29, 2023
  • 1 reply
  • 3321 views

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?

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

Got it makes sense - I'm close but not sure what's incorrect with my output:

## 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) ) && $item.membershipStatus.equals("Member") ) #set( $fullNames = [] ) #set( $void = $fullNames.add($item.fullName) ) ${display.list($fullNames)} #else #end #end

Theoretically I'd think the above should work since everything is within the #foreach loop and the list is set from within the #if conditional - but the display.list output displays the correct values, but not list formatted.


I've also tried putting the output outside of the loop, thinking that the #set within the conditional would set the correct values, but that only outputs 1 of the 2 correct values.


Inside the loop, you’re continually setting $fullNames to an empty list and then populating it with a single item.

1 reply

SanfordWhiteman
New Participant
March 29, 2023

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

 

New Participant
March 29, 2023

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?

SanfordWhiteman
New Participant
March 29, 2023