Use Empty() to see if there is or isn’t a cat in the box

We have all heard of Schrodinger’s cat – the thought experiment, devised by Erwin Schrodinger, wherein “a hypothetical cat may be simultaneously both alive and dead (as a result of being linked to a quantum particle that was in a superposition that may or may not emit radiation per Wikipedia).” Okay, you may not have known all of that, and I am not one to poison cats with radioactive superposition quantum particles. However, the quandary remains – until someone observes the result – the cat is equally dead and alive.

To put this in a less gruesome way – There is a box on the floor across the room. You can not see inside the box from where you are sitting. There is a cat in the house. Until you can see what is in the box – it is equally filled with a cat and not filled with a cat. You have to check the box to see if there is or isn’t a cat in there. If there is a cat in the box, then you have found your cat. If there is not a cat in the box, then you will need to find the treat jar – which until you find it is both full of cat treats and void of cat treats.

What does this have to do with email? Actually, quite a bit. Except it has nothing to do with cats.

You see, we are reliant on data, but know that until we check that field, for that recipient, the data is both there and isn’t there. So how do we ensure that there are no data errors or awkward personalization attempts? Well, it comes down to setting a fallback or a default value if the data we are referencing isn’t there.

We can utilize the AMPscript function EMPTY(). This function’s job is to return true or false based on if there is a value set on that parameter. Before I give you some code examples, let’s look at how this function works.


SYNTAX

Empty(@whattoevaluate)

DEFINITION OF PARTS —

whattoevaluate = This is what you want checked to know if it has a value set or not. It can be passed as a @variable, an [attribute], or nested function (as long as its result is not a rowset or array). If it evaluates as null or empty, the function will output true. Otherwise it will output false.

Simple Examples:

Check to see if @variable is empty:
var @variable
empty(@variable) → true

set @variable=1
empty(@variable) → false

set @variable=””
empty(@variable) → true

empty(substring(“abcdefghijklmnopqrstuvwxyz”,12,7)) → false


Let’s look at a couple of use cases where we can use EMPTY() to help decide which final value to show in the email.

Example 1 – we want to set the CTA text to match their loyalty status –
– Member of program & has points = “Redeem…”
– Member of program & zero points = “Earn…”
– Not yet a member = “Join…”

%%[
VAR @Loyaltypoints, @LoyaltyMember, @cta_text
set @Loyaltypoints= [LoyaltyPoints]
if empty(@Loyaltypoints) then
  set @LoyaltyMemberID = [LoyaltyMemberID]
  if empty(@LoyaltyMemberID) then
    set @cta_text = "Join our Loyalty program"
   else then
    set @cta_text = "Earn more loyalty points"
  endif
 else then
  set @cta_text = "Redeem your Loyalty points"
endif
]%%

The code example above is doing the following:

  1. Declaring the variables
  2. Setting the @Loyaltypoints variable from the data
  3. IF STATEMENT: Checking to see if there is a value in @Loyaltypoints
    • No value in @Loyaltypoints = No Loyalty Points available to redeem –
      1. Setting the @LoyaltyMemberID variable from the data
      2. IF STATEMENT: Checking to see if there is a value in @LoyaltyMemberID
        • No value in @LoyaltyMemberID = No Member ID = Not in program
          1. Setting @cta_text = “Join…”
        • Has value in @LoyaltyMemberID = Is member of program w/ loyalty point balance of 0
          1. Setting @cta_text = “Earn…”
    • Has value in @Loyaltypoints = Has Loyalty points to redeem
      1. Setting @cta_text = “Redeem…”

Example 2 – Special offer only for those with cats –

%%[
var @CatName
set @CatName= [CatName]
if not empty(@CatName) then]%%
We know you love your cat, so we include a free toy/bed with every shipment.  Simply unpack the box and leave sitting out. You won't have to wait long till your cat's curiosity has them playing in the free box that your shipping arrives in.  
%%[endif]%%

The code example above is doing the following:

  1. Declaring the variable
  2. Setting the @CatName variable from the data
  3. IF STATEMENT: Checking to see that @CatName is not empty
    • Is not empty = Has a cat registered with us
    • Closes Ampscript Block
    • Copy to show in email only to those who have cats.
    • Opens Ampscript block, EndIf, Closes block.

References:

Salesforce Developer – Empty()

AMPscript Guide – Empty()

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.