4 Replies Latest reply on Jun 26, 2017 8:57 AM by Phillip Wild

    Email script - isEmpty() not working

    Phillip Wild

      I'm trying to use an #if statement in Velocity to check whether a field is empty. As per Sanford's excellent article here, Marketo imports everything into Velocity as strings, so he suggests using $var.isEmpty() instead of switching between different methods based on data type.

       

      But when I use this statement (and this statement only) in a script:

       

      ${Tour_Evaluation__cList.get(0).Testimonial__c}.isEmpty()

       

      The output I get is either the full testimonial, with the string isEmpty() after it (eg. "I had a great time.isEmpty()") or simply the line "${Tour_Evaluation__cList.get(0).Testimonial__c}.isEmpty()" if the variable truly is empty.

       

      Shouldn't this evaluate to True or False, and display this back to me?

       

      As a second check, I tried (it's a text field):

       

      #if( ${Tour_Evaluation__cList.get(0).Testimonial__c} == "" )

      <b>True</b>

      #else

      <b>False</b>

      #end

       

      This gives me False in all circumstances.

       

      Any ideas?

        • Re: Email script - isEmpty() not working
          Sanford Whiteman

          But when I use this statement (and this statement only) in a script:

          ${Tour_Evaluation__cList.get(0).Testimonial__c}.isEmpty()

          The output I get is either the full testimonial, with the string isEmpty() after it (eg. "I had a great time.isEmpty()") or simply the line "${Tour_Evaluation__cList.get(0).Testimonial__c}.isEmpty()" if the variable truly is empty.

           

          This makes sense, because you're using ${} formal notation but the isEmpty() is outside the curly braces.

           

          Formal notation is meant to separate references (variables/methods) from plain text.

           

          So the right way to express this would be

           

          ${Tour_Evaluation__cList.get(0).Testimonial__c.isEmpty()}
          

           

          However, I want to make sure you know what to expect from isEmpty(). If index [0] does not exist, you can't check its Testimonial__c property for emptiness. Velocity will throw a (handled) exception before it gets there. To check if the index exists, check the Tour_Evaluation__cList.count() before continuing.

          1 of 1 people found this helpful
            • Re: Email script - isEmpty() not working
              Phillip Wild

              Thanks Sanford. Makes sense to me that it needs to be inside the brackets!

               

              Weird thing is...this doesn't seem to have fixed it. I'm still getting "False" for my query where the field clearly seems to be null. Next, I tried it with two leads: one with the testimonial field filled in, one without. The code I used was:

               

              ${Tour_Evaluation__cList.get(0).Testimonial__c}

              ${Tour_Evaluation__cList.get(0).Service__c}

               

              #if( ${Tour_Evaluation__cList.get(0).Testimonial__c.isEmpty()} )

              <b>True</b>

              #else

              <b>False</b>

              #end

               

              The idea is to dump out the Testimonial (if there is one), the Service ID, and then True or False, depending on whether the Testimonial field is empty. The results I got were:

               

              Customer 1 - without Testimonial: ${Tour_Evaluation__cList.get(0).Testimonial__c} a0B1400000YgTa3EAF False

              Customer 2 - with Testimonial: Learned a lot, had a lot of fun, met some very cool people and had a great time. a0Ba000000SVnQ0EAL False

               

              Now in the first case, the output for the Testimonial is null, so the variable name itself has been dumped out. Right? I think that's how it works, anyway. So the final check should give "True". But it doesn't...

               

              I'm testing this by loading the relevant leads into a list and Email Previewing by List, then checking the relevant leads.

               

              Thanks, very much appreciated. Definitely still learning!

                • Re: Email script - isEmpty() not working
                  Sanford Whiteman

                  Don't use ${} formal notation within #if conditions. It's only for output/quoted data.

                    • Re: Email script - isEmpty() not working
                      Phillip Wild

                      Still not working....this is bizarre. But I found a workaround.

                       

                      I tried:

                       

                      #if( $Tour_Evaluation__cList.get(0).Testimonial__c.isEmpty() )

                      <b>True</b>

                      #else

                      <b>False</b>

                      #end

                       

                      This outputs False in Email Preview. To double check the data I tried size(), which outputs 1 for the particular lead I'm looking at. For some reason, the command $Tour_Evaluation__cList.count() gives me the output $Tour_Evaluation__cList.count(), which isn't helpful - but size() works, and logically does the same I'm hoping. So knowing that the size of the list is 1, for further troubleshooting I tried to print the list as well:

                       

                      $Tour_Evaluation__cList.get(0)

                      <br>

                      #if( $Tour_Evaluation__cList.get(0).Testimonial__c )

                      <b>True</b>

                      #else

                      <b>False</b>

                      #end

                       

                      This gives me:

                       

                      {Service__c=a0B1400000YgTa3EAF, Testimonial__c=null}

                      False

                       

                      Since I've effectively reversed the logic (this should evaluate to True if there is a non-null value in that field) I needed to check another lead to make sure it works in the other direction. And checking using a lead that DOES have a Testimonial gave me True, as it should.

                       

                      So....I can get it to work. But I still don't know why isEmpty() isn't giving me the right result, which is annoying. In Velocity, isn't being "null" and being "empty" the same thing?