Conditionals (if statements) using NVelocity

Last Updated: 05/02/2018 Introduced in Version: 2.0

This example simulates the lookup of a customer’s account balance. The NVelocity step uses conditional statements to evaluate the account balance dollar amount and produces a custom message to communicate the account holder’s “status” with the company based on the evaluation (either “Standard,” “Classic Plus,” or “Premium”). The string message output is used to populate an email body sent to the customer.  

 
2016-12-16_131821
 
 
First, in a Designer Folder, create a new flow.
In this example, a Create Data component is used to provide a constant, numeric value to simulate an account balance. In a production environment, this could be a database lookup of an account balance. The Create Data step is found in the Toolbox under the Data category. Drag and drop this component into the workspace. Connect the Start step to the Create Data step.
Click on the Create Data step, and from its Properties panel, configure it as follows:
 
 
Next, add the Custom Merge [NVelocity] component. From the Toolbox, expand the Data > Text category. Drag and drop the Custom Merge [NVelocity] component following the Create or CopyData step. Connect the Create or CopyData step to the Custom Merge [NVelocity] step. Click on the Custom Merge [NVelocity] step.
From the Properties panel, under Merge Setup, type the message to communicate to account holders. For example:
 
 
 
 
 
The conditional statements use the following syntax:

#if($User.IsAnonymous)

You can register now to get the great benefits of membership!

#else

Welcome back $User.UserName!

#end

Additionally, the for each statement supports additional features to enable things like alternating rows, headers, and footers:

#foreach($i in $items)

#each

text which appears for each item

#before

text which appears before each item

#after

text which appears after each item

#between

text which appears between each two items

#odd

text which appears for every other item, including the first

#even

text which appears for every other item, starting with the second

#nodata

Content rendered if $items evaluated to null or empty

#beforeall

text which appears before the loop, only if there are items matching condition

#afterall

text which appears after the loop, only of there are items matching condition

#end

(Source: http://help.ablecommerce.com/mergedProjects/ablecommerce7/index.htm#supplementary/appendix_d_nvelocity_syntax_reference.htm)
 
As variables (i.e. $accountbalance) are entered in the Template field, the MergeInputData field populates. For each variable defined, click on the variable in the MergeInputData field, and click the Edit button. Set the appropriate data type and click OK.
 
 
2016-12-16_130518
Lastly, to complete the configuration of the Custom Merge [NVelocity] step, select the accountbalance Input data (which is the output of the Create or Copy Data step). This is the integer that will be evaluated by the Custom Merge [NVelocity] step.
 
2016-12-16_131437
 
 
To complete this example, the flow will send an email containing the membership status message represented by the output string of the Custom Merge [NVelocity] step, MergedString. From the Toolbox, expand the Communication category and drag and drop a Send Email step into the workspace. Connect the steps as follows to complete the flow:
 
 
2016-12-16_131821
 
 
Configure the email To, From, and Subject line information, and in the Body field, select the Text Merge Plain option. Add the MergedString variable to the Merge Text Editor.
 
 
 
 
 
When the flow runs in the Debugger, an email is sent displaying the membership status as “Classic Plus” since the account balance amount is set to 1,000. Change the value of the Create Data step to different numeric values to see the other if statements of the NVelocity step applied.
 
 

Additional Resources