You are here: Message Personalization and Scripting > Advanced Mail Merge and Tcl Scripting > Conditional Statements

Conditional Statements

 

It is often desirable to modify the contents of a message based on the recipient's stored information. For example, suppose you were required to charge sales tax for purchases made in the state of California? You could use the if command in the following merge tag to insert this information only for recipients who live in California.

 

%%
if { [merge demographics.State] == "CA" } {
 return "\nCalifornia residents must add 8% sales tax.\n" ;
} else {
 return " ;
}
%%

 

In this script, the contents of the demographics.State field is equal to the text string "CA" then the script will return a blank line followed by a line that says "California residents must add 8% sales tax.". Otherwise, it will return nothing. In Tcl, if you want to end a line, you must insert the characters "\n" into your string. In this case, the "\n" at the beginning simply inserts a blank line; the "\n" at the end will make sure that any text that follows this tag will begin on the next line.

 

Suppose you want to insert a different line into your message depending on the time zone that your customer is in? For the continental US, this would require four alternatives. This is also possible using the ifcommand. For example:

 

%%
set timezone [merge demographics.TimeZone] ;
if { $timezone <= -8 } {
 return "10am to 6pm PDT" ;
} elseif { $timezone == -7 } {
 return "9am to 5pm MDT" ;
} elseif { $timezone == -6 } {
 return "8am to 4pm CDT" ;
} else {
 return "7am to 3pm EDT" ;
}
%%

 

This example assumes that your TimeZone field is an integer that defines the number hours past Greenwich Mean Time your customer is in.  

 

These examples demonstrate the basic if command functionality. Note that the else and elseifparts of the if command are optional. For example, the first example could have also been simplified to:

 

%%
if { [merge demographics.State] == "CA" } {
 return "\nCalifornia residents must add 8% sales tax.\n" ;
}
%%

 

It is good to get into the habit of explicitly returning an empty string, however. As noted in the introduction, not doing so can result in a script returning something that you did not expect.

 

Another useful conditional command is the switch command. The switch command simplifies the certain types of if elseif else type commands. For example, suppose you wanted to return a state name in place of a state code. This example will be limited to just a few conditions but it is equally valid with more. Using the if command, you could do this:

 

%%
set state [merge demographics.State] ;
if { $state == "CA" } {
 return "California" ;
} elseif { $state == "OR" } {
 return "Oregon" ;
} elseif { $state == "WA" } {
 return "Washington" ;
} elseif { $state == "NV" } {
 return "Nevada" ;
} else {
 return "Unknown" ;
}
%%

 

This is obviously very hard to read and quickly gets out of hand. You can accomplish the same thing using the switch command. The above example then becomes:

%%
switch [merge demographics.State] {
 "CA" { return "California" }
 "OR" { return "Oregon" }
 "WA" { return "Washington" }
 "NV" { return "Nevada" }
 default { return "Unknown" }
}
%%

 

Expressions

It is often useful to be able to do mathematical calculations using data specific to each recipient. This can be accomplished using the expr command. For example:

 

%%
set balance [merge accounts.Receivable] ;
set penalty [expr 0.10 * $balance] ;
return "If your account balance is not paid in 10 days, please include a late fee of \$$penalty." ;
%%

 

Notice that since we wanted to use the dollar sign "$", we had to escape it using a back-slash in order to keep it from being interpreted as the beginning of a string.