SOLVED

I Broke My Adapted Velocity Scripts

Go to solution
Liz_Davalos
Level 3

I Broke My Adapted Velocity Scripts

First I'll say I've already received significant help from many people here on getting my toes wet in velocity scripting. Here's some I adapted for my purposes (that other's shared) and I somehow broke:

The point of this is to only show the token data if they have a city in the system:

#if( !$lead.City.isEmpty() && !$lead.Inferred_City.isEmpty() )

   #set ($nearlocation = "")

#elseif ( !$lead.City.isEmpty() )

#set ($nearlocation = " Near ${lead.Inferred_City}")

#else

#set ($nearlocation = " Near ${lead.City}")

#end

${nearlocation}

This token is for a sentence that either ends with their first name (if we have it) or just ends.

#if (!$lead.FirstName.isEmpty())

  #set ($greeting = "")

#else

  #set ($greeting = " ${lead.FirstName}")

#end

${greeting}

Thank you, thank you, thank you, to anyone who can help me solve this, ETA 10ish days total now working with Velocity Scripts.

1 ACCEPTED SOLUTION

Accepted Solutions
Liz_Davalos
Level 3

Re: I Broke My Adapted Velocity Scripts

Yes, the boxes are checked (although I didn't know that was required so helpful for the future). I just edited my question so it should show as code now.

View solution in original post

6 REPLIES 6
SanfordWhiteman
Level 10 - Community Moderator

Re: I Broke My Adapted Velocity Scripts

Have you ensured that the fields you're using checked off in the tree on the right-hand-side of Script Editor?

Also, please highlight your Velocity snippets as Java using the Advanced Editor's Syntax Highlighter. Otherwise code is very difficult to read.

pastedImage_2.png

pastedImage_3.png

Liz_Davalos
Level 3

Re: I Broke My Adapted Velocity Scripts

Yes, the boxes are checked (although I didn't know that was required so helpful for the future). I just edited my question so it should show as code now.

SanfordWhiteman
Level 10 - Community Moderator

Re: I Broke My Adapted Velocity Scripts

Yes, it is required so the fields will be present in the Velocity context.

While your code looks runnable, I wonder about your logic here:

#if (!$lead.FirstName.isEmpty()) 

This means "If the statement First Name is empty is not true..." -- in other words, "If First Name is not empty..."

Yet when this condition matches, you're setting the greeting to the empty string.

Are you sure you don't have this backwards?

Liz_Davalos
Level 3

Re: I Broke My Adapted Velocity Scripts

Oh wow, I think that's it, I thought it was if that field was empty then, else...

SanfordWhiteman
Level 10 - Community Moderator

Re: I Broke My Adapted Velocity Scripts

Leading ! -- in all languages I can think of where it's valid -- means (boolean) negation, that is, true to false or false to true.

isEmpty() returns boolean true if a string is empty, so you're flipping the value before checking it.

Liz_Davalos
Level 3

Re: I Broke My Adapted Velocity Scripts

Ah ok, I didn't know the meaning, that explains a lot. Written that way it actually simplifies my script. Thank you so much, I'm running with this!