IIF condition

Simplified Personalization: Mastering the IIF() Function in AMPscript

IIF() is a shorthand conditional function that returns one value if a condition is TRUE and another value if the condition is FALSE.

Syntax

IIF(condition, TrueValue, FalseValue)

IIF is good for Simple two-way decisions.

Use Case 1: Personalized Greeting

%%[
VAR @FirstName, @LastName, @Gender, @Greetings
SET @FirstName=AttributeValue("FirstName")
SET @LastName=AttributeValue("LastName")
SET @Gender=AttributeValue("Gender")

SET @Greetings = IIF( EMPTY(@Gender),"Customer",IIF(@Gender=="Male","Mr.","Ms"))
]%%

Dear %%=v(@Greetings)=%% %%=v(@FirstName)=%%

Use Case 2: Loyalty Member Classification

Business Requirement

Classify customers based on their membership status.

Rules

MemberStatus Output
Gold Premium Benefits
Others Standard Benefits
%%[
VAR @FirstName, @LastName, @MemberStatus, @Membership
SET @FirstName=AttributeValue("FirstName")
SET @LastName=AttributeValue("LastName")
SET @MemberStatus=AttributeValue("MemberStatus")

SET @Membership = IIF(@MemberStatus=="Gold","Premium Customer",IIF(@MemberStatus=="Silver","VIP Customer","Standard Customer"))
]%%

Dear %%=v(@FirstName)=%% You are our %%=v(@Membership)=%%

Use Case 3: Dynamic Discount Assignment

Business Requirement

Provide different discounts based on total purchase amount.

Rules

TotalPurchase Discount
Greater than 10000 20% OFF
10000 or Less 10% OFF

Why Companies Use This

Reward high-spending customers.

%%[
VAR @FirstName, @TotalPurchase, @Greetings
SET @FirstName = AttributeValue("FirstName")
SET @TotalPurchase = AttributeValue("TotalPurchase")

SET @Greetings = IIF(@TotalPurchase>10000, "10% Discount", "5% Discount")
]%%

Hi %%=v(@FirstName)=%% You got %%=v(@Greetings)=%% on your purchase

Use Case 4: Cart Abandonment Journey

Why Companies Use This

Cart abandonment is one of the most common marketing use cases.

Example:

Customer adds products to cart
↓
Does not complete checkout
↓
Reminder Email Sent

Business Requirement

Show a different message depending on whether the customer abandoned their cart.

Rules

IsCartAbandoned Message
True Complete Your Purchase Today
False Thank You For Shopping
%%[
VAR @FirstName, @isCartAbandoned, @Greetings
SET @FirstName = AttributeValue("FirstName")
SET @isCartAbandoned = AttributeValue("isCartAbandoned")

SET @Greetings = IIF(@isCartAbandoned=="TRUE", "Complete Your Purchase Today", "Thank You For Shopping")
]%%

Hello %%=v(@FirstName)=%% 
%%=v(@Greetings)=%%

Use Case 5: Mobile App Adoption

Business Requirement

Display different messages based on whether the customer uses the mobile app.

Rules

IsAppUser Message
Yes Exclusive App Offer
No Download Our App Today

Why Companies Use This

Mobile app users are more engaged and generate more revenue.

Examples:

Existing App User

Exclusive App Offer
Get Early Access To New Features

Non-App User

Download Our App Today
Get ₹100 Welcome Bonus
%%[
VAR @FirstName, @IsAppUser, @Greetings
SET @FirstName = AttributeValue("FirstName")
SET @IsAppUser = AttributeValue("IsAppUser")

SET @Greetings = IIF(@IsAppUser=="TRUE", "Exclusive App Offer", "Download Our App Today")
]%%

Hello %%=v(@FirstName)=%% 
%%=v(@Greetings)=%%

Use Case 6: Dynamic Subject Line

Business Requirement

Change the email subject line based on customer engagement.

Rules

OpenCount Subject Line
Greater than 5 We Miss You Less!
5 or Less We Miss You!
%%[
VAR @FirstName, @OpenCount, @Greetings
SET @FirstName = AttributeValue("FirstName")
SET @OpenCount = AttributeValue("OpenCount")

SET @Greetings = IIF(@OpenCount>5, "We Miss You less!", "We Miss You!")
]%%

Hello %%=v(@FirstName)=%% 
%%=v(@Greetings)=%%

Why is IIF() Used?

IIF() is used to write simple IF-ELSE logic in one line.

Feature IIF() IF-ELSE
Syntax One line Multiple lines
Readability Good for simple logic Good for complex logic
Conditions Usually 1 condition Multiple conditions
ELSEIF Support ❌ No ✅ Yes
Nested Logic Possible but messy Easy
Performance Similar Similar
Best Use Simple True/False Business rules

IIF() is a shorthand conditional function used for simple two-outcome decisions and returns a value directly. IF-ELSE provides full conditional control, supports ELSEIF and complex business logic, and is preferred for multi-condition personalization scenarios.

IIF() = Simple Decision (2 outcomes)
IF-ELSEIF-ELSE = Complex Decision (Multiple outcomes)

⚠️ Important Architectural Considerations for IIF()

  • Eager Evaluation Catch: Unlike standard block logic structures where non-matching conditional branches are completely ignored, AMPscript evaluates both parameters (the TrueValue and the FalseValue) during compilation. If either argument contains another nested function execution that throws an exception, your entire email send will break—even if that particular branch wasn't selected.
  • Keep Nesting Minimal: While code blocks like IIF(cond, A, IIF(cond2, B, C)) work perfectly fine, chaining more than two layers deep quickly ruins maintainability. When logic branches scale, refactoring to full block IF/ELSEIF layouts will significantly clean up team workflows.

Comments

Popular posts from this blog

IF Condition use cases

AMPscript IF–THEN–ELSEIF–ELSE–ENDIF Notes and Use case