New to Velocity Script and having trouble with #if statement | Community
Skip to main content
New Participant
November 24, 2020
Solved

New to Velocity Script and having trouble with #if statement

  • November 24, 2020
  • 1 reply
  • 4199 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman
#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

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
November 24, 2020
#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
cslaterAuthor
New Participant
November 24, 2020

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
New Participant
November 24, 2020

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