14 Replies Latest reply on Dec 15, 2016 10:15 AM by Sanford Whiteman

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

    Charles THIERY

      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

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

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

           

          eg:

          <ul>

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

              <li>Performance</li>

          #end

          </ul>

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

              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?

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

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

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

                      Still not working...

                       

                      An error occurred when procesing the email Body!

                      Encountered "{" near

                      <ul>
                      #if(${{lead.Interest__c}}.indexOf('Performance'))
                          <li>Performance</li>
                      #end
                        • Re: Email script - ${lead.Interest} contains "A"
                          Sanford Whiteman

                          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.

                          1 of 1 people found this helpful
                            • Re: Email script - ${lead.Interest} contains "A"
                              Geoff Krajeski

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

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

                                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

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

                                    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).

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

                                        Hi Sandford,

                                         

                                        What is the way to make the negative test of what I have previously asked and with ";" instead of "," please? I thought by Adding "!" at the beginning would work... but it seems to not working:

                                        1. #set( $item = "Performance" )
                                        2. #set( $item2 = "Talent" )
                                        3. #if( !$lead.Interest__c.matches("(?i)(.*)(^|;)\s*${item}\s*(;|$)(.*)") ) 
                                        4. #elseif( !$lead.Interest__c.matches("(?i)(.*)(^|;)\s*${item2}\s*(;|$)(.*)") ) 
                                        5. #else

                                         

                                        Sorry, you become a kind of my guru!

                                         

                                        BR,

                                        Charles

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

                                            When you reach the point of two lookups as opposed to a one-off, you don't want to repeat the same code. This is known the as the DRY principle in programming. As you will see below, the moment you add a second check, you want to refactor the whole approach because otherwise you will eventually ***** up due to mistyping, and people will have trouble reading your code.

                                             

                                            (There's nothing about two mostly-identical strings that says, "These are always supposed to differ only by this one character": they might as well differ by 100 characters. Totally identical strings in different lines of code also can't inform the reader -- which doesn't have to be someone else, it could be you a year from now  -- "Whatever string you put in Line 2 must be repeated in Line 4.")

                                             

                                            Another incredibly useful way to frame your thinking is the Zero-One-Infinity principle.  For our purposes here, ZOI means that once you decide you want two lookups, you have to assume there will be a much higher number of lookups (maybe not "infinity" as the principle suggests, but the idea is "more than 1 = much more than 1"). ZOI means you should treat 2 as if it were 20, 200, or 2000. Imagine how crazy your script token would get if you copy-and-pasted-and-slightly-altered your code just 20 times.  Obey the DRY principle accordingly.

                                             

                                            ## generic list-like string scanner w/selectable delimiter
                                            #macro( listContains $haystack $needle $delim )
                                            #if( $haystack.matches("(?i)(.*)(${delim}|^)\s*${needle}\s*(${delim}|$)(.*)") )
                                            true##
                                            #else
                                            false##
                                            #end
                                            #end
                                            ## convenience call w/semicolon
                                            #macro( semiListContains $haystack $needle )
                                            #listContains( $haystack $needle ';' )
                                            #end
                                            ## convenience call w/comma
                                            #macro( commaListContains $haystack $needle )
                                            #listContains( $haystack $needle ',' )
                                            #end
                                            #if( "#semiListContains( $lead.Interest__c 'Talent' )" == "true" )
                                            Does contain Talent.
                                            #end
                                            #if( !("#semiListContains( $lead.Interest__c 'Performance' )" == "true") )
                                            Doesn't contain Performance.
                                            #end
                                            
                                            1 of 1 people found this helpful