This is a implementation case which has been described in Sanfords blog post at:
https://blog.teknkl.com/velocity-poison-pill-throw-error-revisited/
I want to block sending an email if my velocity script has no output.
The idea is the use a global macro which has been defined in a Global Velocity token ( {{my.velocity includes}} )
#macro( throw_v2 $message )
#if( !$message.isEmpty() )
#evaluate( "${esc.h}set( ${message} )" )
#end
#end
In my email I include this token like {{my.velocity includes}}
Further in my email I include the next simple code ( as example )
#set( $output = '' )
#set( $nrOfLinksInOutput = 0 )
## render generated content
#if( $nrOfLinksInOutput == 0)
#throw_v2("ineligible")
#else
#set( $output = 'Links available' )
#end
${output}
In preview mode this code is executed properly, but when I try to approve my mail I receive the next error :
Validation Error approving OPER-3649-UPSELL-2022.TESTMAIL3 — <div>An error occurred when procesing the email Body! </div> <p>Encountered "ineligible" near</p> <div><pre ><p>#macro( throw_v2 $message )</pre><pre > #if( !$message.isEmpty() )</pre><pre class="x-form-item-label"> #evaluate( "${esc.h}set( ${message} )" )</pre><pre > #end</pre><pre >#end</p> #set( $output = '' )</pre></div>
Can someone help me here? What am I doing wrong?
Solved! Go to Solution.
I hope you included the code to print the output in a separate velocity token and included it in the email, right? I tried this in my SB and it worked fine for me - I created two script tokens one having the macro and another having the business logic to render the generated content and calling the macro if required to stop the send.
Script Token - 1
#macro( throw_v2 $message )
#if( !$message.isEmpty() )
#evaluate( "${esc.h}set( ${message} )" )
#end
#end
Script Token - 2
##static value set in the $nrOfLinksInOutput variable for the purpose of testing
#set($nrOfLinksInOutput = 0)
#if($nrOfLinksInOutput.equals(0))
#throw_v2("ineligible")
#else
#set($output = 'Links available')
#end
${output}
I included both the script tokes in an email, and tried sending it to a test lead and the email encountered soft bounce as expected, then I changed the value of the $nrOfLinksInOutput to a non-zero value in the script token, and as expected in that case the email went out fine, i.e. did not encounter any soft bounce.
I hope you included the code to print the output in a separate velocity token and included it in the email, right? I tried this in my SB and it worked fine for me - I created two script tokens one having the macro and another having the business logic to render the generated content and calling the macro if required to stop the send.
Script Token - 1
#macro( throw_v2 $message )
#if( !$message.isEmpty() )
#evaluate( "${esc.h}set( ${message} )" )
#end
#end
Script Token - 2
##static value set in the $nrOfLinksInOutput variable for the purpose of testing
#set($nrOfLinksInOutput = 0)
#if($nrOfLinksInOutput.equals(0))
#throw_v2("ineligible")
#else
#set($output = 'Links available')
#end
${output}
I included both the script tokes in an email, and tried sending it to a test lead and the email encountered soft bounce as expected, then I changed the value of the $nrOfLinksInOutput to a non-zero value in the script token, and as expected in that case the email went out fine, i.e. did not encounter any soft bounce.
Hi Darshil,
First of all, thank you for taking the time to look at my case, i really appreciate it.
I have put your above suggested code into 2 new tokens and the email is working.
I can't pinpoint the differences exactly, but at least I can build on this working part.
Cool, glad that it's working now. 🙂