Email script - isEmpty() not working | Community
Skip to main content
Level 9
June 23, 2017
Question

Email script - isEmpty() not working

  • June 23, 2017
  • 1 reply
  • 3029 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

SanfordWhiteman
Level 10
June 23, 2017

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.

Level 9
June 23, 2017

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!

SanfordWhiteman
Level 10
June 23, 2017

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