3 Replies Latest reply on Nov 6, 2017 9:17 PM by Sanford Whiteman

    Weird Issue with Velocity Scripting

    Hobie Thompson

      I have a Velocity script that works in one campaign, but not in another one. I'm racking my brain trying to figure out what the heck is happening here.  There are two different Velocity scripts that are included in the email I am trying to send out. The way the script is supposed to work is off of a data value, lead.SecurityQuizScore. Below, see a screenshot of a test I just did where the Security Quiz Score value is properly computed (meaning my scoring campaign is functioning) to '2':

       

      score.PNG

      Here is the part of the HTML code for the email that is going out that contains my Velocity tokens:

       

      html-email.PNG

       

      And here are the two Velocity scripts themselves, contained in the Program in which the email is located:

       

      PercentageLgl:

      percentagelgl.PNG

      ScriptLgl:

      scriptLgl.PNG

       

      Unless I'm missing something, in the above example, the email should load with $imgUrl as the '66percent-250px.png' image and with $testing as 'High'. However, in the email I receive, both of these are set to their fallbacks.

       

      Any idea what is going on here? What am I missing?

        • Re: Weird Issue with Velocity Scripting
          Sanford Whiteman

          You have the field checked off in the tree on the right-hand-side of Script Editor, I hope?

           

          Also, default isn't supported with Velocity tokens (choose the default output within the script itself).

           

          Finally, your VTL can be much, much simpler (and thus easier to troubleshoot):

           

          #set( $imgUrl = "http://your.default.image.url/goes/here" )
          #set( $scoresToRisk = {
            ["6","12","18"] : "100",
            ["5","11","17"] : "100"
          } )
          #foreach( $scores in $scoresToRisk.keySet() )
          #if( $scores.contains($lead.securityQuizScore) )
          #set( $imgUrl = "http://your.image.url/is/${scoresToRisk[$scores]}.png" )
          #break
          #end
          #end
          
          1 of 1 people found this helpful
            • Re: Weird Issue with Velocity Scripting
              Hobie Thompson

              D'oh! *whaps forehead* I didn't realize the Data Value had to be checked in the Velocity script in order to be used. That solved that problem!

               

              And forgive the sloppy coding! Definitely not my forte.

                • Re: Weird Issue with Velocity Scripting
                  Sanford Whiteman

                  I didn't realize the Data Value had to be checked in the Velocity script in order to be used. That solved that problem!

                  Yeah, that's a necessary evil... if all lead fields were automatically exported into Velocity it would be a tragic memory situation.

                   

                  And forgive the sloppy coding! Definitely not my forte.

                  Definitely get to know Velocity/Java HashMaps. They'll change the way you write conditions in Velocity: I haven't written a chain of #if-#elseif-#elseifs in ages. In some other environment, they might be overengineering, but Velocity's crazy verbosity makes it essential to find ways to write leaner code. In fact in your conditions above there are a couple of bugs, probably from having data too spread out (see the unreachable code for securityQuizScore 10 and 16).

                  1 of 1 people found this helpful