AttributeValue and empty fields: AMPscript personalization that never breaks a send

Use AttributeValue, Empty and IIF to personalize emails safely when a field does not exist or arrives empty in the Data Extension.

White Tutorial Marketing Cloud Engagement 3 min Published

Personalizing an email with %%FirstName%% looks simple until the day the field arrives empty, or the same email is reused with a Data Extension that does not even have that column. The result is a "Hi, ," in the customer's inbox, or a validation error at send time. This tutorial shows the safe way to read attributes in AMPscript.

The problem with %%Field%%

When you write %%FirstName%% directly in the HTML, Marketing Cloud looks for an attribute with that name in the send source (the sendable Data Extension or the subscriber profile). Two things can go wrong:

  • the attribute does not exist in that source: email validation fails and the send does not go out;
  • the attribute exists but is empty: the email goes out with a hole in the text.

AttributeValue: read without breaking

AttributeValue("FieldName") returns the attribute value and, if the attribute does not exist, returns an empty string instead of raising an error. That makes it the starting point for any reusable personalization.

AMPscript
%%[
SET @name = AttributeValue("FirstName")
]%%

Empty: handle the blank

Empty(@variable) returns true when the variable is null or an empty string. Combine it with IF to set a default:

AMPscript
%%[
SET @name = AttributeValue("FirstName")

IF Empty(@name) THEN
  SET @name = "there"
ELSE
  SET @name = ProperCase(Trim(@name))
ENDIF
]%%
Hi %%=v(@name)=%%!

Trim removes leading and trailing spaces (very common in data imported from spreadsheets) and ProperCase capitalizes the first letter of each word: "MARIA" becomes "Maria".

IIF: the one-line version

For simple cases, IIF(condition, value_if_true, value_if_false) saves the whole IF block:

AMPscript
%%[
SET @name = AttributeValue("FirstName")
SET @greeting = IIF(Empty(@name), "Hi!", Concat("Hi ", ProperCase(@name), "!"))
]%%
%%=v(@greeting)=%%

Subject line and preheader too

The same care applies to the subject line, which is where a "Hi, ," shows up the most. Define the variables in an AMPscript block in the email body and use %%=v(@greeting)=%% in the subject field: the subject line can read variables defined in the content.

Quick checklist

Situation What to use
The field may not exist in the Data Extension AttributeValue("Field")
The field may arrive empty Empty(@var) with a default value
Text coming from an import Trim() and ProperCase()
A simple decision in one line IIF()
Print a variable in the HTML or subject %%=v(@var)=%%

Summary

Read attributes with AttributeValue, handle blanks with Empty and always have a default value. That way the same email works with any Data Extension and no customer ever gets a broken greeting.

Related

Related
Title Type Platform Belt Reading
How to use LookupRows in AMPscript (and when to switch to Lookup or LookupOrderedRows) Tutorial Marketing Cloud Engagement Yellow 3 min
Data Views in SQL: who opened or clicked in the last 30 days Tutorial Marketing Cloud Engagement Green 3 min
Deduplicate records with ROW_NUMBER() in a Query Activity Snippet Marketing Cloud Engagement Green 2 min
Marketing Cloud REST API: OAuth 2.0 authentication and writing to a Data Extension Tutorial Marketing Cloud Engagement Brown 3 min