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
THEN do something else
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.
IF child is not gounded
THEN child can have a bowl of ice cream for dessert
THEN no dessert
IF I were to put this into AMPscript it could look like this:
THEN @dessert="ice cream"
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.
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>
Here is the IF() function syntax and a few more examples.
SYNTAX from DEVELOPER.SALESFORCE.COM
Using IF Syntax
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.
%%[ 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”
set @subjectline=”Donate to help animals in your area”
Advanced Example 1 – AMPscript Block:
if @favoritecolor == "blue" then
elseif @favoritecolor == “green” then
elseif @favoritecolor == “red” then
else then set @backgroundcolor=”#FFFFFF”
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.
if <condition> then
ELSE IF EXAMPLE
else if <condition>then