0

I am attempting to add dynamic content to an email and want to remove the velocity syntax from the email body and replace it with a $var by using the #set directive like this:

#if('x' = 'y') #set($z = 'z') #elseif( 'a' = 'b') #set($c = 'c')

This was my attempt:

 #if( {$date_opened_5ca62c21c455a} == "" && {$firstName} != "" )
 #set($header_greeting = "{$firstName}, Community Member.")
 #elseif( {$date_opened_5ca62c21c455a} != "" && {$firstName} != "" )
 #set( $header_greeting = "{$firstName}, member since 
 {$date_opened_5ca62c21c455a}")
 #else
 #set( $header_greeting = "Building Tomorrow Together" )
 #end

I received an error saying a closing ) was expected

I also took a shot in the dark with:

 #set( $header_greeting = "#if( {$date_opened_5ca62c21c455a} == "" && {$firstName} != "" ){$firstName}, Community Member.#{elseif({$date_opened_5ca62c21c455a} != "" && {$firstName} != "")}{$firstName}, member since {$date_opened_5ca62c21c455a}#{else}Building Tomorrow Together#end" )

2 Answers2

0

You missed the correct formal notation syntax, it's :

${foo}

and not

{$foo}

but as a side note, you don't seem to need the formal notation in your example, since it's only used in ambiguous cases, aka. $foo.bar against ${foo}.bar.

Also, your first example uses = instead of == for equality.

Among other things, you must also verify that the missing values are empty strings, and not just missing values. $foo renders to $foo and not the empty string if the value is missing. To check of null or empty, you can use the silent notation:

#if("$!foo" == "") ...

With all these remarks, your attempt can be rewritten as:

#if( "$!date_opened_5ca62c21c455a" == "" && "$!firstName" != "" )
  #set( $header_greeting = "$firstName, Community Member." )
#elseif( "$!date_opened_5ca62c21c455a" != "" && "$!firstName" != "" )
  #set( $header_greeting = "$firstName, member since $date_opened_5ca62c21c455a" )
#else
  #set( $header_greeting = "Building Tomorrow Together" )   
#end

Last but not least, the default behavior of #if($foo) is to return false if $foo is null or empty, so you can write:

#if( !$date_opened_5ca62c21c455a && $firstName )
  #set( $header_greeting = "$firstName, Community Member." )
#elseif( $date_opened_5ca62c21c455a && $firstName )
  #set( $header_greeting = "$firstName, member since $date_opened_5ca62c21c455a" )
#else
  #set( $header_greeting = "Building Tomorrow Together" )   
#end
Claude Brisson
  • 4,085
  • 1
  • 22
  • 30
  • Thanks for you reply Claude, these values({$date_opened_5ca62c21c455a} and {$firstName}) are the merge tags(lead field vales) in SharpSpring. I was able to get it working by using string concatenation with the merge tags vs using them inside the the quotation marks. – Jane Fiscal Aug 19 '19 at 15:24
0

This is the solution for getting merge tags to function with the set() directive with velocity scripting in SharpSpring crm. The issue was using the merge tags inside of the string instead of being concatenated with it.

#if( {$date_opened_5ca62c21c455a} == "" && {$firstName} != "" )

#set( $headerGreeting = {$firstName} + ", Community Member." )

#elseif( {$date_opened_5ca62c21c455a} != "" && {$firstName} != "" )

#set( $headerGreeting = {$firstName} + ", member since " + {$date_opened_5ca62c21c455a} )

#else

#set( $headerGreeting = "Building Tomorrow Together" )

#end