SOLVED

How do I write Velocity script using "any" or "or" conditions?

Go to solution
Highlighted
Level 4

How do I write Velocity script using "any" or "or" conditions?

I'm trying to combine NEW Product X Trials ($lead.dMSSubscribedDate) and NEW Product X Subscriptions ($lead.dMSTrialDateTime) using "any" or "or" conditions utilizing the (||).

 

If a Lead's Trial OR Subscription DateTime is in past 24 hours then that New Product Trial or Subscription = TRUE and will receive the corresponding Welcome content.

 

I think this is an easy function within velocity based on how seamlessly the use of the "and" (&&) function was to utilize.

The challenge is writing un-broken code. I can't get the output correct.

 

I want to combine this 1st block with the 2nd block using the "any" or "or" (| |) logic:

#set( $ISO8601WithSpace = "yyyy-MM-dd HH:mm:ss" )
#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/Los_Angeles") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601 = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#if( !$lead.dMSTrialDateTime.isEmpty() )
#set( $caldMSTrialDateTime = $convert.toCalendar(
 $convert.parseDate(
 $lead.dMSTrialDateTime, 
 $ISO8601WithSpace, 
 $defaultLocale, 
 $defaultTimeZone 
 )
) )
#if( $date.difference($calNow,$caldMSTrialDateTime).getHours() >= -24 )
You now have access to PRODUCT X!
#end
#end
#set( $ISO8601WithSpace = "yyyy-MM-dd HH:mm:ss" )
#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/Los_Angeles") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601 = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#if( !$lead.dMSSubscribedDate.isEmpty() )
#set( $caldMSSubscribedDate = $convert.toCalendar(
 $convert.parseDate(
 $lead.dMSSubscribedDate, 
 $ISO8601WithSpace, 
 $defaultLocale, 
 $defaultTimeZone 
 )
) )
#if( $date.difference($calNow,$caldMSSubscribedDate).getHours() >= -24 )
You now have access to PRODUCT X!
#end
#end

 I do know this line is correct ---

#if( $date.difference($calNow,$caldMSSubscribedDate).getHours() >= -24 || $date.difference($calNow,$caldMSTrialDateTime).getHours() >= -24 )

--- however I incorrectly assumed that in every line $lead.dMSSubscribedDate and $caldMSSubscribedDate appeared (the first #if and #set lines 9-17) would require the | | followed by the trial fields $lead.dMSTrialDateTime and $caldMSTrialDateTime.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Level 6

Re: How do I write Velocity script using "any" or "or" conditions?

Hi Ronn

 

So you have the right idea. 

 

I would think of it this way. 

 

You start from this logic block.

 

#if($condition1 = true)
###do something
#end

#if($condition2 = true)
##do the same thing
#end

 

So your code to combine the conditions will be:

 

#if($condition1 = true || $condition2 = true)
###do something
#end

 

The main thing is that your doing a date conversion on different fields, so I would personally get that out of the way early (since it's outside of the "or" logic), and then do your comparison at the end. Something like:

 

##set global variables

#set( $ISO8601WithSpace = "yyyy-MM-dd HH:mm:ss" )
#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/Los_Angeles") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601 = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )

##end global variables

##check that dMSTrialDateTime isn't empty. If so, convert to a date format.

#if( !$lead.dMSTrialDateTime.isEmpty() )
#set( $caldMSTrialDateTime = $convert.toCalendar(
 $convert.parseDate(
 $lead.dMSTrialDateTime, 
 $ISO8601WithSpace, 
 $defaultLocale, 
 $defaultTimeZone 
 )
) )
#end

##check that $lead.dMSSubscribedDate isn't empty. If so, convert to a date format.

#if( !$lead.dMSSubscribedDate.isEmpty() )
#set( $caldMSSubscribedDate = $convert.toCalendar(
 $convert.parseDate(
 $lead.dMSSubscribedDate, 
 $ISO8601WithSpace, 
 $defaultLocale, 
 $defaultTimeZone 
 )
) )
#end

##we now have two variables, if they exist - and we can compare on both of them.

#if( $date.difference($calNow,$caldMSTrialDateTime).getHours() >= -24 || $date.difference($calNow,$caldMSSubscribedDate).getHours() >= -24)
You now have access to PRODUCT X!
#end

 

I hope I haven't missed any brackets but I think that will do the trick. Note that this assumes that you ALWAYS have a value in those fields that can be parsed into a date. If they can't, it will throw an error and the email won't send. Similarly, if the last condition is false (neither field is within 24 hours) then nothing will be output. You could create #else conditions in the if statements to take care of that. 

 

Hope that helps! The general idea is to only keep the steps that are required in the if statement,  and take all the commonalities out. The global variables are the same for both blocks of code, so no point writing it twice.

View solution in original post

4 REPLIES 4
Highlighted
Level 6

Re: How do I write Velocity script using "any" or "or" conditions?

Hi Ronn

 

So you have the right idea. 

 

I would think of it this way. 

 

You start from this logic block.

 

#if($condition1 = true)
###do something
#end

#if($condition2 = true)
##do the same thing
#end

 

So your code to combine the conditions will be:

 

#if($condition1 = true || $condition2 = true)
###do something
#end

 

The main thing is that your doing a date conversion on different fields, so I would personally get that out of the way early (since it's outside of the "or" logic), and then do your comparison at the end. Something like:

 

##set global variables

#set( $ISO8601WithSpace = "yyyy-MM-dd HH:mm:ss" )
#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/Los_Angeles") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601 = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )

##end global variables

##check that dMSTrialDateTime isn't empty. If so, convert to a date format.

#if( !$lead.dMSTrialDateTime.isEmpty() )
#set( $caldMSTrialDateTime = $convert.toCalendar(
 $convert.parseDate(
 $lead.dMSTrialDateTime, 
 $ISO8601WithSpace, 
 $defaultLocale, 
 $defaultTimeZone 
 )
) )
#end

##check that $lead.dMSSubscribedDate isn't empty. If so, convert to a date format.

#if( !$lead.dMSSubscribedDate.isEmpty() )
#set( $caldMSSubscribedDate = $convert.toCalendar(
 $convert.parseDate(
 $lead.dMSSubscribedDate, 
 $ISO8601WithSpace, 
 $defaultLocale, 
 $defaultTimeZone 
 )
) )
#end

##we now have two variables, if they exist - and we can compare on both of them.

#if( $date.difference($calNow,$caldMSTrialDateTime).getHours() >= -24 || $date.difference($calNow,$caldMSSubscribedDate).getHours() >= -24)
You now have access to PRODUCT X!
#end

 

I hope I haven't missed any brackets but I think that will do the trick. Note that this assumes that you ALWAYS have a value in those fields that can be parsed into a date. If they can't, it will throw an error and the email won't send. Similarly, if the last condition is false (neither field is within 24 hours) then nothing will be output. You could create #else conditions in the if statements to take care of that. 

 

Hope that helps! The general idea is to only keep the steps that are required in the if statement,  and take all the commonalities out. The global variables are the same for both blocks of code, so no point writing it twice.

View solution in original post

Highlighted

Re: How do I write Velocity script using "any" or "or" conditions?


Note that this assumes that you ALWAYS have a value in those fields that can be parsed into a date. If they can't, it will throw an error and the email won't send. Similarly, if the last condition is false (neither field is within 24 hours) then nothing will be output. You could create #else conditions in the if statements to take care of that. 

 

Hope that helps! The general idea is to only keep the steps that are required in the if statement,  and take all the commonalities out. The global variables are the same for both blocks of code, so no point writing it twice.


Actually there won't be a fatal error (and soft bounce) in this case. $caldMSTrialDateTime et al. will be null so in turn the difference condition won't match. But the email will still go out.

Highlighted
Level 4

Re: How do I write Velocity script using "any" or "or" conditions?

This worked to perfection! Thanks! I've tested every permutation of Trial / Subscribed / Past 24 hours / over 24 hours ago and it deploys the emails with the appropriate messaging flawlessly.

 

I did actually attempt this logic block sequence at first but it returned the soft bounce i.e. broken syntax error. Of course, not even knowing if that was the proper way to do it I wasn't sure if it was a minor broken code error or if the entire logic block sequence method was wrong.

 

In the end - it most likely returned the syntax error simply because I neglected to properly close those blocks with #end

 

Thank you @SanfordWhiteman for all of your help - our entire onboarding and Welcome Series is in place thanks to you helping me utilize Velocity. I can't tell you how grateful I am.

 

 

Highlighted
Level 6

Re: How do I write Velocity script using "any" or "or" conditions?

Agree, without Sanford I would have no idea how to do any of this. Velocity, and the UI Marketo gives you, is not a forgiving programming interface!