We started the conversation about one of the most used and most powerful AMscript functions- the IF statement.
The IF Function simplified is:
IF condition is true
THEN do something
ELSE
THEN do something else
ENDIF
We’ve gone over the first part – conditions. So you now know the different ways to decide if something is true of not true. Why this is so important is that all the logic in an if statement is built off of it.
We use “if statements” in our lives all the time and don’t realize it. Even children understand this kind of logic.
For instance:
IF child is not gounded
THEN child can have a bowl of ice cream for dessert
ELSE
THEN no dessert
ENDIF
IF I were to put this into AMPscript it could look like this:
EXAMPLE 1: %%[
IF @grounded==false
THEN @dessert="ice cream"
ELSE
THEN @dessert="nothing"
ENDIF
]%%
In Example 1 – we used the IF to set 2 different values for the same variable based on a condition. We can also use the IF in the body HTML – see example #2. Using IF to control content blocks can give you the opportunity to collapse blocks, have multiple blocks under one if, or use multiple conditions.
EXAMPLE 2:
I decided to cook with my kit.%%[
IF @grounded==false THEN ]%%
<a href=”https://www.delish.com/content/dessert-recipes/“> Since they are not grounded we made a delish dessert.</a>
%%[ ELSE THEN ]%%
<a href”https://www.delish.com/cooking/g1409/brussels-sprouts-recipes/”> Since they are grounded I picked a recipe for brussel sprouts. </a>
%%[ENDIF]%%
Here is the IF() function syntax and a few more examples.
SYNTAX from DEVELOPER.SALESFORCE.COM
Using IF Syntax
if <condition>
then <statement>
elseif <condition>
then <statement>
else
then <statement>
endif
Definition of parts —
IF – Use the IF statement to perform conditional processing
ELSEIF – Use the ELSEIF statement to perform conditional processing. Multiple ELSEIF statements can appear within an IF block.
ELSE – Use the ELSE statement to perform conditional processing. Only one else statement can appear within an IF block.
ENDIF – Use the ENDIF statement to end an IF block. Only one ENDIF statement must appear at the end of an IF block.
Simple Example:
%%[ if @pet=="cat" then
set @subjectline="Purrfection for your feline pal"
elseif @pet==”dog” then
set @subjectline=”How much do you know about dogs? Take our pup-quiz”
elseif @pet==”bird” then
set @subjectline=”Toys you can really tweet home about”
else then
set @subjectline=”Donate to help animals in your area”
endif
]%%
Advanced Example 1 – AMPscript Block:
%%[
if @favoritecolor == "blue" then
set @backgroundcolor=”#0000FF”
elseif @favoritecolor == “green” then
set @backgroundcolor=”#008000″
elseif @favoritecolor == “red” then
set @backgroundcolor=”#FF0000″
else then set @backgroundcolor=”#FFFFFF”
endif
]%%
ELSEIF vs ELSE IF
So these are two different things.
ELSEIF is part of a the current IF statement.
ELSE IF is adding a 2nd IF statement as part your statement- and will require it’s own ENDIF statement.
ELSEIF EXAMPLE
if <condition> then
<statement>
elseif <condition>then
<statement>
else
then
<statement>
endif
ELSE IF EXAMPLEif <condition>
then
<statement>
else if <condition>then
<statement>
else then
<statement>
endif
endif