Re: Marketo tokens - commas and spaces when using several lead tokens

Anonymous
Not applicable

Marketo tokens - commas and spaces when using several lead tokens

Hi there,

We are wanting to list several data fields within an eDM using lead data tokens. However, some of these data fields may be sitting empty, and we are unsure what to do regards to commas and spacing between the tokens. Does anyone have a workaround for this?

Thanks in advance, Tina.

Tags (1)
2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

Re: Marketo tokens - commas and spaces when using several lead tokens

Please move the thread to Products and we'll continue over there. "Community" is for website feedback, not product support -- and yes, this is confusing!

SanfordWhiteman
Level 10 - Community Moderator

Re: Marketo tokens - commas and spaces when using several lead tokens

Tina, I didn't hear back from you, so I'm going to give you the answer before I step away for the night. But please don't respond until you've moved the thread to the right place.

Sounds like you want a comma-delimited list of tokens, but you don't want any extraneous delimiters if a token is empty. This is a common situation. You'll do this using an Email Script token (a.k.a. Velocity token).

When creating Velocity tokens, always make sure you check off the fields you need on the right-hand-side tree. Here's a quick guide to using the Script Token Editor:

114_vtl_editor_markedup.png

And the code you want is:

#set( $allPossibleFields = [ $lead.field1, $lead.field2, $lead.field3, $lead.field4 ] )

#set( $delim = ", " )

#set( $lastDelim = ", and " )

## ---- NO NEED TO EDIT BELOW THIS LINE! ----

#set( $filledFields = [] )

#foreach( $field in $allPossibleFields )

#if( !$field.isEmpty() )

    #set( $tmp = $filledFields.add($field) )

#end

#end

$display.list($filledFields, $delim, $lastDelim)

$lead.field1 - $lead.field4 represent all possible fields in your list (obviously "field1," "field2," etc. are replaced with the real Velocity names of the fields in your instance). You can have more than four, that's just an example.

$delim is the delimiter between all items but the last two (in the example, it's a comma and a space).

$lastDelim is the special delimiter between the last two items.  Here I've used an Oxford comma. You could use the same value for $delim and $lastDelim.

So if you had four fields and the third one is empty, sample output would be:

one, two, and four