Salesforce Marketing Cloud AMPscript Conditional Flow
Salesforce Marketing Cloud AMPscript Conditional Flow: Complete Guide with Real-World Use Cases
AMPscript conditional statements are used to control personalization, dynamic content, subscriber eligibility, offer assignment, localization, and journey routing in Salesforce Marketing Cloud. These conditions execute on the server before an email is sent, ensuring every subscriber receives relevant content.
1. IF Statement
What is IF Statement?
The IF statement executes a block of code only when a condition evaluates to TRUE.
Syntax
%%[
IF condition THEN
/* code */
ENDIF
]%%
Eligibility & Compliance Checks (Most Common)
Use Case
A financial services company wants to show a credit card offer only to customers who are 18 years or older. If the customer is below 18 or the age is unavailable, the offer should be restricted.
Business Applications
Age-based offers
Gambling & betting compliance
Alcohol-related campaigns
Banking & financial product eligibility
COPPA / GDPR consent validation
Algorithm
START
Read Age
IF Age is NOT EMPTY
AND Age >= 18
Display "You can access this offer"
ELSE
Display "Offer restricted"
ENDIF
END
AMPscript Code
2️⃣ Personalization (Gender-Specific Greeting)
Use Case
A retail company wants to personalize email greetings based on the subscriber's gender.
Male subscribers should receive "Dear Mr."
Female subscribers should receive "Dear Ms."
If gender is unavailable, a generic greeting should be displayed.
Business Applications
Personalized email greetings
Customer engagement campaigns
Welcome email journeys
Promotional email campaigns
Subject line personalization
Algorithm
START
Read Gender
IF Gender = Male
Display "Dear Mr."
ELSE IF Gender = Female
Display "Dear Ms."
ELSE
Display "Dear Customer"
ENDIF
END
AMPscript Code
%%[
VAR @Gender,@msg,@Name
SET @Name = AttributeValue("FirstName")
SET @Gender = AttributeValue("Gender")
IF @Gender == "Male" THEN
SET @msg = CONCAT("Mr." ,@Name)
ELSEIF @Gender == "Female" THEN
SET @msg = CONCAT("Ms.",@Name)
ELSE
SET @msg = "DEAR CUSTOMER"
ENDIF
]%%
%%=v(@msg)=%%Expected Output
Record 1
Gender = Male
Output
Record 2
Gender = Female
Output
Record 3
Gender = NULL
Output
Offer & Promotion Logic (Loyalty-Based Discounts)
Use Case
An e-commerce company wants to reward customers based on their loyalty tier.
Gold members receive a 30% discount.
Silver members receive a 20% discount.
All other customers receive a 10% discount.
This helps businesses increase customer retention and encourage customers to move to higher loyalty tiers.
Business Applications
Coupon generation
Promotional discounts
Loyalty programs
Exclusive member campaigns
Customer retention offers
Algorithm
START
Read LoyaltyStatus
IF LoyaltyStatus = Gold
Assign 30% Discount
ELSE IF LoyaltyStatus = Silver
Assign 20% Discount
ELSE
Assign 10% Discount
ENDIF
Display Discount
END
AMPscript Code
%%[ VAR @Tier,@dis SET @Tier = AttributeValue("LoyaltyStatus") IF @Tier == "Gold" THEN SET @dis = "40%" ELSEIF @Tier == "Platinum" THEN SET @dis = "20%" ELSE SET @dis = "10%" ENDIF ]%% Congratulations! You have unlocked a discount of %%=v(@dis)=%%Expected Output
Record 1
LoyaltyStatus = Gold
Output
Record 2
LoyaltyStatus = Silver
Output
Record 3
LoyaltyStatus = Bronze
Output
Localization & Language Handling
Use Case
A global company wants to send emails in the subscriber's preferred language based on their country.
Customers from India should receive content in Hindi.
Customers from France should receive content in French.
All other customers should receive content in English.
This improves customer experience by delivering content in a familiar language and ensures regional relevance.
Business Applications
Multi-language email campaigns
Regional promotions
Country-specific legal disclaimers
Global customer journeys
Personalized content experiences
Algorithm
START
Read Country
IF Country = IN
Language = Hindi
ELSE IF Country = FR
Language = French
ELSE
Language = English
ENDIF
Display Content In Selected Language
END
AMPscript Code
%%[
VAR @lang
SET @lang = AttributeValue("Language")
IF @lang == "EN" THEN
SET @lang = "ENGLISH"
ELSEIF @lang == "HI" THEN
SET @lang = "HINDI"
ELSE
SET @lang = "FRENCH"
ENDIF
]%%
Preferred Language:- %%=v(@lang)=%%
Expected Output
Record 1
@lang == "HI"
Output
Record 2
@lang = "FR"
Output
Record 3
@lang == "EN"
Output
Channel Suppression (Email Consent Management)
Use Case
A company wants to send marketing emails only to subscribers who have explicitly opted in to receive email communications.
If a subscriber has given consent, the email can be sent.
If consent is not available, the subscriber should be suppressed from marketing communications.
Business Applications
- CAN-SPAM compliance
- GDPR compliance
- Opt-in based communication
- Preference centers
- Email suppression management
Algorithm
START
Read EmailOptIn
IF EmailOptIn = True
Allow Email Send
ELSE
Suppress Subscriber
ENDIF
ENDAMPscript Code
%%[
VAR @EmailOptIn,@sendEmail
SET @EmailOptIn = AttributeValue("EmailOptIn")
IF @EmailOptIn == "True" THEN
SET @sendEmail = "Yes"
ELSE
SET @sendEmail = "No"
ENDIF
]%%
Email Eligible: %%=v(@sendEmail)=%%Expected Output
Record 1
EmailOptIn = TrueOutput
Record 2
EmailOptIn = FalseOutput
Dynamic CTA / URL Handling
Use Case
A company wants to provide different landing page experiences based on the subscriber's device.
- Mobile users should be redirected to a mobile-optimized website.
- Desktop users should be redirected to the standard website.
This improves user experience and increases conversion rates by sending users to the most appropriate destination.
Business Applications
- Mobile vs Desktop experience
- App deep linking
- Device-specific landing pages
- Campaign tracking URLs
- Conversion optimization
Algorithm
START Read Device IF Device = Mobile Redirect to Mobile Website ELSE Redirect to Desktop Website ENDIF Display CTA Link ENDAMPscript Code
%%[ VAR @Device,@ctaLink SET @Device = AttributeValue("Device") IF @Device == "Mobile" THEN SET @ctaLink = "https://m.site.com" ELSE SET @ctaLink = "https://site.com" ENDIF ]%% <a href="%%=RedirectTo(@ctaLink)=%%">Shop Now</a>Expected Output
Record 1
Device = MobileOutput
CTA URL: https://m.site.comRecord 2
Device = DesktopOutput
CTA URL: https://site.comData Validation & Null Handling (Critical)
Use Case
A company wants to ensure emails do not break when important subscriber information is missing.
- If EmailAddress is missing, mark the record as invalid.
- If City is missing, display a default location.
- Prevent blank content caused by NULL values.
This is one of the most important defensive coding practices in Salesforce Marketing Cloud.
Business Applications
- Data quality validation
- Preventing broken emails
- Default fallback values
- Dynamic content safety
- Production-ready personalization
Algorithm
START Read EmailAddress IF EmailAddress is EMPTY Mark Record as Invalid ENDIF Read City IF City is EMPTY Set City = "Your Area" ENDIF Display Final Values ENDAMPscript Code
%%[ VAR @EmailAddress,@City,@status SET @EmailAddress = AttributeValue("EmailAddress") SET @City = AttributeValue("City") IF EMPTY(@EmailAddress) THEN SET @status = "Invalid Record" ELSE SET @status = "Valid Record" ENDIF IF EMPTY(@City) THEN SET @City = "Your Area" ENDIF ]%% Status: %%=v(@status)=%%<br> City: %%=v(@City)=%%Behavioral Targeting (Engagement-Based Segmentation)
Use Case
A company wants to segment subscribers based on their email engagement activity.
- Subscribers who opened an email within the last 30 days are considered Active.
- Subscribers who have not opened an email in the last 30 days are considered Inactive.
This segmentation helps marketers send the right message to the right audience at the right time.
Business Applications
- Re-engagement campaigns
- Win-back journeys
- Content throttling
- Audience segmentation
- Customer retention programs
Algorithm
START Read LastOpenDays IF LastOpenDays <= 30 Segment = Active ELSE Segment = Inactive ENDIF Display Segment ENDAMPscript Code
%%[ VAR @LastOpenDays,@segment SET @LastOpenDays = AttributeValue("LastOpenDays") IF @LastOpenDays <= 30 THEN SET @segment = "Active" ELSE SET @segment = "Inactive" ENDIF ]%% Customer Segment: %%=v(@segment)=%%Expected Output
Record 1
LastOpenDays = 10Output
Customer Segment: ActiveRecord 2
LastOpenDays = 25Output
Customer Segment: ActiveRecord 3
LastOpenDays = 90Output
Customer Segment: InactiveTransactional Emails (Order Status Handling)
Use Case
An e-commerce company wants to send different transactional messages based on the customer's order status.
- If the order is confirmed, send an order confirmation message.
- If the order is cancelled, send a cancellation notification.
- For all other statuses, send an order processing update.
Transactional emails are critical because customers rely on them for real-time updates about their purchases.
Business Applications
- Order confirmation emails
- Order cancellation notifications
- Payment failure emails
- Refund notifications
- Shipping and delivery updates
Algorithm
START Read OrderStatus IF OrderStatus = Confirmed Display Order Confirmation Message ELSE IF OrderStatus = Cancelled Display Order Cancellation Message ELSE Display Order Processing Message ENDIF ENDAMPscript Code
%%[ VAR @OrderStatus,@msg SET @OrderStatus = AttributeValue("OrderStatus") IF @OrderStatus == "Confirmed" THEN SET @msg = "Your order is confirmed." ELSEIF @OrderStatus == "Cancelled" THEN SET @msg = "Your order was cancelled." ELSE SET @msg = "Your order is being processed." ENDIF ]%% %%=v(@msg)=%%Expected Output
Record 1
OrderStatus = ConfirmedOutput
Your order is confirmed.Record 2
OrderStatus = CancelledOutput
Your order was cancelled.Record 3
OrderStatus = ProcessingOutput
Your order is being processed.Journey Decision Pre-Processing
Use Case
A company wants to identify high-value customers before they enter a Journey.
Customers with a score of 80 or above are tagged as High Value. This flag can then be used as an Entry Source attribute or Journey Decision Split condition.
Instead of repeatedly evaluating complex conditions inside Journey Builder, AMPscript can prepare the flag beforehand.
Business Applications
- High-value customer journeys
- VIP campaign routing
- Lead qualification
- Customer lifecycle journeys
- Journey entry segmentation
Algorithm
START Read Customer Score IF Score >= 80 Assign Path = High Value ELSE Assign Path = Standard Value ENDIF Display Journey Path ENDAMPscript Code
%%[ VAR @Score,@path SET @Score = AttributeValue("Score") IF @Score >= 80 THEN SET @path = "High Value" ELSE SET @path = "Standard Value" ENDIF ]%% Journey Path: %%=v(@path)=%%Expected Output
Record 1
Score = 95Output
Journey Path: High ValueRecord 2
Score = 82Output
Journey Path: High ValueRecord 3
Score = 65Output
Journey Path: Standard ValueBoolean Logic (AND / OR / NOT)
Use Case
A financial company wants to determine whether a customer is eligible for a premium investment offer.
To qualify:
- Customer must be 18 years or older.
- Customer must belong to India.
- Customer must have a valid phone number.
Additionally, customers can be contacted through Email or SMS channels.
This type of logic combines multiple conditions using AND, OR, and NOT operators.
Business Applications
- Complex segmentation
- Compliance validation
- Multi-condition targeting
- Audience qualification
- Preference center management
Algorithm
START Read Age Read Country Read Mobile Read PreferredChannel IF Age >= 18 AND Country = IN AND Mobile is NOT EMPTY Eligible Customer ELSE Not Eligible ENDIF IF PreferredChannel = Email OR PreferredChannel = SMS Reachable Customer ELSE Unsupported Channel ENDIF ENDAMPscript Code
%%[ VAR @Age,@Country,@Mobile,@Channel,@msg SET @Age = AttributeValue("Age") SET @Country = AttributeValue("Country") SET @Mobile = AttributeValue("Mobile") SET @Channel = AttributeValue("PreferredChannel") IF @Age >= 18 AND @Country == "IN" AND NOT EMPTY(@Mobile) THEN SET @msg = "Eligible Customer" ELSE SET @msg = "Not Eligible" ENDIF ]%% %%=v(@msg)=%%Expected Output
Record 1
Age = 25 Country = IN Mobile = 9876543210Output
Eligible CustomerRecord 2
Age = 16 Country = IN Mobile = 9876543210Output
Not EligibleRecord 3
Age = 28 Country = IN Mobile = NULLOutput
Not EligibleNested IF (Advanced Logic)
Use Case
A financial institution wants to display an investment offer only to customers who:
- Belong to India.
- Are 18 years or older.
The age check should only happen if the customer is from India.
This is a classic Nested IF scenario where the second condition depends on the first condition being true.
Business Applications
- Country-specific eligibility checks
- Banking and financial compliance
- Region-based offers
- Regulatory restrictions
- Advanced personalization rules
Algorithm
START Read Country IF Country = IN Read Age IF Age >= 18 Display "Eligible in India" ELSE Display "Underage Customer" ENDIF ELSE Display "Offer Not Available" ENDIF ENDAMPscript Code
%%[ VAR @Country,@Age,@msg SET @Country = AttributeValue("Country") SET @Age = AttributeValue("Age") IF @Country == "IN" THEN IF @Age >= 18 THEN SET @msg = "Eligible in India" ELSE SET @msg = "Underage Customer" ENDIF ELSE SET @msg = "Offer Not Available" ENDIF ]%% %%=v(@msg)=%%Expected Output
Record 1
Country = IN Age = 25Output
Eligible in IndiaRecord 2
Country = IN Age = 16Output
Underage CustomerRecord 3
Country = US Age = 30Output
Offer Not Available
Comments
Post a Comment