How do I write Velocity script using "any" or "or" conditions? | Community
Skip to main content
New Participant
February 21, 2020
Solved

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

  • February 21, 2020
  • 1 reply
  • 4280 views

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.

 

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 PhillipWi1

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.

1 reply

PhillipWi1Accepted solution
New Participant
February 24, 2020

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.

SanfordWhiteman
New Participant
February 24, 2020

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.