SOLVED

Email script - ${lead.Interest} contains "A"

Go to solution
Highlighted

Email script - ${lead.Interest} contains "A"

Hello,

I don't manage to find on the documentation a way to use VTL in order to make a #if conditions for searching a specific value coming from a Marketo field.

Does anybody could help me on this please?

eg:

<ul>

#if(${lead.Interest}.contains("Performance"))

    <li>Performance</li>

#end

</ul>

BR,

Charles

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Level 10 - Community Moderator

Re: Email script - ${lead.Interest} contains "A"

Should post to Products, not Champions (this space is to discuss the Champions program itself).

For a case-sensitive partial match you want

#if( $lead.Interest__c.contains("Performance") )

Note this will also match on "Power, LowPerformancer, Strength".

Matching that is less prone to error uses a regex:

#set( $item = "Performance" )

#if( $lead.Interest__c.matches("(?i)(.*)(^|,)\s*${item}\s*(,|$)(.*)") )

In general, "list-like" strings should be avoided unless you can make absolutely sure that the delimiter ("," in this case) cannot appear unescaped within an item. That is, if an item has a comma inside it -- which is totally possible with a string unless you control all the input -- parsing becomes problematic. Instead use a known structure like a JSON array.

(Geoff, you don't need curlies around a token if it isn't enclosed within quotes or output.)

ETA: Putting the matching item in a separate var will be more maintainable.

View solution in original post

14 REPLIES 14
Highlighted
Level 7 - Champion Alumni

Re: Email script - ${lead.Interest} contains "A"

You should likely be able to use ${{my.token}}.indexOf('A')

eg:

<ul>

#if(${lead.Interest}.indexOf("Performance"))

    <li>Performance</li>

#end

</ul>

Highlighted

Re: Email script - ${lead.Interest} contains "A"

Hi Geoff,

Thanks for the quick reply but I got this error:

An error occurred when procesing the email Body!

Lexical error, Encountered: "i" (105), after : "." at *unset*[line 90, column 25] near

<ul>
#if(${lead.Interest}.indexOf("Performance"))
    <li>Performance</li>
#end
</ul>

Maybe, I should clarify what I'm looking for:

My field ${lead.Interest} contains value like "A, B, C, D, ..." and I want to test on a specific value for creating a bullet point.

Any guess on why it's not working please?

Highlighted
Level 7 - Champion Alumni

Re: Email script - ${lead.Interest} contains "A"

woops.... need double curly braces around the token... my bad

Highlighted

Re: Email script - ${lead.Interest} contains "A"

Still not working...

An error occurred when procesing the email Body!

Encountered "{" near

<ul>
#if(${{lead.Interest__c}}.indexOf('Performance'))
    <li>Performance</li>
#end
Highlighted
Level 10 - Community Moderator

Re: Email script - ${lead.Interest} contains "A"

Should post to Products, not Champions (this space is to discuss the Champions program itself).

For a case-sensitive partial match you want

#if( $lead.Interest__c.contains("Performance") )

Note this will also match on "Power, LowPerformancer, Strength".

Matching that is less prone to error uses a regex:

#set( $item = "Performance" )

#if( $lead.Interest__c.matches("(?i)(.*)(^|,)\s*${item}\s*(,|$)(.*)") )

In general, "list-like" strings should be avoided unless you can make absolutely sure that the delimiter ("," in this case) cannot appear unescaped within an item. That is, if an item has a comma inside it -- which is totally possible with a string unless you control all the input -- parsing becomes problematic. Instead use a known structure like a JSON array.

(Geoff, you don't need curlies around a token if it isn't enclosed within quotes or output.)

ETA: Putting the matching item in a separate var will be more maintainable.

View solution in original post

Highlighted
Level 7 - Champion Alumni

Re: Email script - ${lead.Interest} contains "A"

So, it is best to use the SFDC/API name, Sandy? i.e.. Interest__c?

Highlighted
Level 10 - Community Moderator

Re: Email script - ${lead.Interest} contains "A"

Well, I'd say use the VTL name, since you have to select the prop from the tree of fields anyway.

Highlighted

Re: Email script - ${lead.Interest} contains "A"

Hello Sandford,

Thank you for your answer. it works perfectly as long as I have the delimiter "," with or without space next to it.

I'm just wondering how it would have been possible for me to find your answer on my own. Do you have a documentation or website to recommend me please?

I'm really amazed by your add "(?i)(.*)(^|,)\s*" & "\s*(,|$)(.*)" which is definitely something that I can't understand.

BR,

Charles

Highlighted
Level 10 - Community Moderator

Re: Email script - ${lead.Interest} contains "A"

Hi Charles! Glad it's working.

I recommend my blog posts on Velocity (http://blog.teknkl.com/tag/velocity) or really my whole blog. I'm the only person regularly posting on this development stuff with a Marketo focus.

I try to write for the power user who wants to become a junior developer, as opposed to for full-blown techies, so the path to learning is somewhat easier.

Velocity is a simplified subset of Java, but that means it's simpler than... one of the most complex and mature modern programming languages! The programmer-y roots are always there. For example, the contains() and matches() methods above are Java String methods, which are best learned from Java docs/books/blogs, even though those reference materials are targeted at professional programmers as opposed to "email scripters."

The pattern in my matches() call is a regular expression, and a pretty simple/clunky one at that. It means "zero or more characters, followed by either the start of a string or a comma, followed by zero or more spaces, followed by the word 'Performance', followed by zero or more spaces, followed by either the end of the string or a comma, followed by zero or more characters." So describing the ways in which your list-like string can contain "Performance" and accounting for human error (extra spaces) and any order or number of items. Oh, and case-insensitive (?i).