SOLVED

Re: New to Velocity Script and having trouble with #if statement

Go to solution
cslater
Level 2

New to Velocity Script and having trouble with #if statement

I think the issue is that it's reading ${lead.previous_billing_period_previews} as a string instead of as a number, so that's why nothing is triggering the if statement, but I don't know how to switch it to a number in velocity script.

 

Here's what I've got:

#set( $preview = $number.integer(${lead.previous_billing_period_previews}) )
#if( ${lead.previous_billing_period_previews} > 1000 )
at $preview,
#else
nearing your previews limit,
#end


So if someone has more than 1000 previews it should display their previews, but instead it's always displaying "nearing your previews limit" I've tried several different variations, but haven't found anything that works. Any help would be welcome.

2 ACCEPTED SOLUTIONS

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: New to Velocity Script and having trouble with #if statement

#set( $previews = $convert.toInteger( $lead.previous_billing_period_previews ) )
#if( $previews > 1000 )
at ${previews},
#else
nearing your previews limit,
#end

 

  • you need to use $convert.toInteger, not $number.integer
  • $number.integer formats a Number or numeric String to a String, not what you want
  • even if $number.integer did work, you still weren't using the converted value, but the original variable
  • remember to not use ${formal} references unless you're outputting content

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: New to Velocity Script and having trouble with #if statement


This mostly works, I still need the preview to be a string when I display it

There's no difference between (a) stringifying a Number first and (b) outputting a Number directly in Velocity. It's always converted to a String upon output.

 


when the value of $lead.previous_billing_period_previews is null, then I get an error message. 

It's never null, but an empty String, which is extremely different. And null isn't a valid keyword in Velocity, so your code above will have a fatal error.

 

If you need to account for the empty case, then check for it explicitly.

#if( $lead.previous_billing_period_previews.isEmpty() )
#set( $previews = 0 )
#else
#set( $previews = $convert.toInteger( $lead.previous_billing_period_previews ) )
#end
#if( $previews > 1000 )
at ${previews},
#else
nearing your previews limit,
#end

 

View solution in original post

5 REPLIES 5
SanfordWhiteman
Level 10 - Community Moderator

Re: New to Velocity Script and having trouble with #if statement

#set( $previews = $convert.toInteger( $lead.previous_billing_period_previews ) )
#if( $previews > 1000 )
at ${previews},
#else
nearing your previews limit,
#end

 

  • you need to use $convert.toInteger, not $number.integer
  • $number.integer formats a Number or numeric String to a String, not what you want
  • even if $number.integer did work, you still weren't using the converted value, but the original variable
  • remember to not use ${formal} references unless you're outputting content
cslater
Level 2

Re: New to Velocity Script and having trouble with #if statement

This mostly works, I still need the preview to be a string when I display it and when the value of $lead.previous_billing_period_previews is null, then I get an error message. So I need to set up something to deal with those values. I'm doing something wrong, but I'm not sure what.

#set( $preview = $number.integer($lead.previous_billing_period_previews) )

#if( $lead.previous_billing_period_previews === null )
  #set ( $previewInt = 0 )
#else
  #set( $previewInt = $convert.toInteger($lead.previous_billing_period_previews) )
#end 

#if( $previewInt > 1000 )
    at ${preview},
#else
    nearing your previews limit,
#end




 

SanfordWhiteman
Level 10 - Community Moderator

Re: New to Velocity Script and having trouble with #if statement


This mostly works, I still need the preview to be a string when I display it

There's no difference between (a) stringifying a Number first and (b) outputting a Number directly in Velocity. It's always converted to a String upon output.

 


when the value of $lead.previous_billing_period_previews is null, then I get an error message. 

It's never null, but an empty String, which is extremely different. And null isn't a valid keyword in Velocity, so your code above will have a fatal error.

 

If you need to account for the empty case, then check for it explicitly.

#if( $lead.previous_billing_period_previews.isEmpty() )
#set( $previews = 0 )
#else
#set( $previews = $convert.toInteger( $lead.previous_billing_period_previews ) )
#end
#if( $previews > 1000 )
at ${previews},
#else
nearing your previews limit,
#end

 

cslater
Level 2

Re: New to Velocity Script and having trouble with #if statement

Thank you. I do need to include the number.integer as that's how I get the comma to display in the number. But the isEmpty() is what I needed.

SanfordWhiteman
Level 10 - Community Moderator

Re: New to Velocity Script and having trouble with #if statement

OK. Just to be clear or for any lurkers, $number.integer outputs a formatted String with a preset NumberFormat.

 

But simply outputting an Integer ${variable} also creates a String!

 

(There's no way to "output" an Integer variable into the body of a Text/HTML email as an Integer, as it's a text-only format. It's always a String representation of the Integer.)