Coupon Claiming in AMPscript

One cool thing you can do in the Salesforce Marketing Cloud is offer your subscribers a unique coupon code as part of a promotional email campaign. Here’s how to do it.

To start, create a “CouponCodes” Data Extension with the following columns:
coupon-claimrow2

Next, generate your own list of unique coupon codes in Excel. Include a “CouponCode” column header. Save the worksheet as CSV or Unicode (tab-delimited).
coupon-claimrow1

Then, import your coupons into the Data Extension with the toolbar import button:
coupon-claimrow3

Finally, add this AMPscript to your email. This code will try to find a coupon row for the specified email address in the CouponCodes data extension. If it finds one, it’ll return the coupon code. If not, it will “claim” an unclaimed code and associate it with the specified email address. The bit about the message context — that will prevent you from claiming coupon rows when you’re doing a Send Preview when building the email.

Keep these things in mind:

  • Make sure you upload enough coupons for your target audience so you don’t run out of unclaimed coupons. For recurring coupon campaigns, you should build a system to monitoring unused coupon codes.
  • Make sure the target of the coupon code (e-commerce site, POS system, etc) is using the same set of coupon codes as your campaign.
  • You can also do this in an SMS campaign.
  • The data extension name in the claimrow() function cannot be a variable
  • Any data extension referenced in a claimrow() function must exist

For further reading:

Data Extensions
Data Extension AMPscript Functions
MobileConnect Coupon Scenario
Live Offers

Update 5/28/2013: Added additional gotchas about claimrow() function
Update 2/9/2015: Added empty() check after claimrow() function
Update 3/9/2016: Move code snippet to gist, added variable declaration for emailaddr
Update 7/24/2017: Removed unnecessary lookup. Thanks, Brian (see comments below).
Update 6/4/2019: fix Data Extension name mismatch

AMPScript Lookup Examples

In Marketing Cloud, there are several ways to retrieve external data with AMPscript inside your email, landing page or SMS message. Here are few ways to go about it.

Suppose you have a non-sending Data Extension that contains these rows:

LookupColumnDEColumn1DEColumn2
wheeNeoKeanu Reeves
wheeTrinityCarrie-Anne Moss
wheeMorpheusLaurence Fishburne
whoaAgent SmithHugo Weaving

Lookup value of single column value from a certain row

Output

DEColumn1 is Agent Smith

Lookup multiple column values from a single row

Output

DEColumn1 is Neo, DEColumn2 is Keanu Reeves

Lookup multiple column values from multiple ordered rows

Output


Row 1, DEColumn1 is Trinity, DEColumn2 is Carrie-Anne Moss
Row 2, DEColumn1 is Neo, DEColumn2 is Keanu Reeves
Row 3, DEColumn1 is Morpheus, DEColumn2 is Laurence Fishburne

For further reference:

Updates

  • 2017-10-19 – Added example Data Extension and output
  • 2018-08-24 – update reference links