I'm starting with AMPscript, and I need to send a batch of email messages to my customers where each one receives a slightly different content, which should be obtained dynamically for each email

To clarify, it's not a random value, is something I'll know in advance and is a value that needs to be unique between customers.

To simplify, let's say that the first email I sent to the first customer in my batch has this text (the number is just an example, the real value is non-numeric, non-sequential and I know it in advance):

Hello, customer #1 !

The second email in the batch, should look like this:

Hello, customer #2 !

And so on, where each email should have a different unique value (#3, #4, ...) Is this possible with AMPscript? Where should I put the dynamic information? (In my example, that would be the values 1, 2, 3, 4, ...) I'm a bit confused with the documentation, because they clearly state that:

When writing your AMPscript, always remember that all calls will process at the end of the subscriber batch when performing email sends

And I interpret the above as "the script is executed exactly once" at the end of a batch of messages, making it impossible to implement my use case. Is my interpretation correct?

  • if you want to display a dynamic random number, then you can just write logic of random number in ampscript in the email and every customer will receive an email with a dynamic random number. But if you want a sequential/incremental number to be displayed in the email, then it can't be done in ampscript. For this, it is best to generate these sequential/incremental numbers in a SQL query, update your DE and then use that DE field in your email. Hope this helps. Feb 13, 2019 at 12:06
  • @PraveenKumarBandi My use case falls into the second category - it's not a random number, it's a value that's different for each customer. Assuming that I'm using a DE, how could I retrieve a different value per email, using AMPscript? Feb 13, 2019 at 13:01
  • 1
    You can easily do this using personalization strings. Let's say Customer is your send DE and it has a field "Coupon", you can just write %%Coupon%% in your email, the code will be populated in the email when it is sent out. Feb 15, 2019 at 3:13

3 Answers 3


It almost sounds like you need a “voucher solution”, where ClaimRow is your friend.

You need to build a Data Extension with few columns, as described in the documentation, one of them being populated with the values you want to send out to your recipients. In the email, you need to put code similar to this below, taking one of the values and assigning to a recipient. Hence you can later look up, what value was sent to who.

%%[VAR @CouponRow
SET @CouponRow = ClaimRow('Coupon', 'IsClaimed', 'JobID', JobID, 'ListID', ListID, 'BatchID', _JobSubscriberBatchID, 'SubscriberID', SubscriberID)
IF EMPTY(@CouponRow) THEN ]%%
No coupons available
%%[ ELSE ]%%
Your Coupon Code is %%=FIELD(@CouponRow,'CouponCode') =%%
%%[ ENDIF ]%%

Some additional help can be found here:


Send a coupon - Claim row

Unique coupon codes to same subscriber

  • 1
    Yes! the "voucher solution" is similar enough to my use case. Thanks for suggesting this approach, Lukas! Feb 15, 2019 at 10:09
  • 1
    Why didn't you just write it in your question? ;-) Feb 15, 2019 at 10:46
  • 1
    Great job reading between the lines and finding the optimum solution @LukasLunow! Feb 15, 2019 at 13:54
  • 1
    Thank you @Gortonington - doing what I can for SFSE Feb 15, 2019 at 20:39

As @JohannesSchapdick stated, the AMPScript is run once per subscriber and is executed right before being pushed into the send queue. This means you cannot pass a value from one subscriber to the next directly.

To be able to implement something that includes an ordinal numbering system, you have a couple options:

You first can just upload the values into your sendable DE and reference them there if they are pre-existing.

Option 1: (preferred if you do not have pre-assigned values.)

  1. Create a new field inside your DE named 'UniqueCode'
  2. Create a SQL query to fill this row in via SQL's NewID() function
  3. Inside your email, you reference the corresponding field using AttributeValue()

For example:

SQL (very basic sample)

NewID() AS UniqueCode
FROM [your DE]
/*Target: [your DE] */
/*Action: update */

AMPscript and output:

   set @UniqueCode = AttributeValue("UniqueCode")


Option 2 (If you need a unique value, but do not want to assign until send time)

You need to include the new field inside your DE, similar to Option1. Use the AMPscript GUID() function to fill in a random value. You then upsert this value to the DE for future reference.


    SET @GUID = GUID()
    UPSERTDATA("yourDE", 1, "yourPkey", yourPkey, "UniqueCode", @GUID)

Option 3: (Best if you have pre-assigned codes and do not want to include in your sendable DE)

In this option you would use a separate DE with 2 columns and 1 row. You then would do Lookups and Upserts to manipulate this DE.

  1. Create a DE with the following rows PrimaryKey | UniqueCode. You would add in each PKey from your original DE.
  2. Upload your list of unique codes assigned to your Pkeys.
  3. Push in AMPscript to lookup this value

Example AMPScript:

In beginning of email (before number needs to be used)

    SET @uniqueCode = Lookup("yourLookupDE", "UniqueCode", "Pkey", yourPkey)


I edited my answer to include options of codes outside of ordinal.

  • Thanks for the info! But just to be clear: the value I need to include is not ordinal in nature (that was just an example in the question). I cannot calculate the "next" value, it's more like a unique "code" I need to associate with each email. That's not the core of the question, the core is: how can I get such a value for each email? Feb 13, 2019 at 15:12
  • 1
    @ÓscarLópez - edited my answer to match your needs in comments Feb 13, 2019 at 15:29

You need to know which kind of rules are the reason that your customers receive different content.

You could just create a dynamic content block with these rules to achieve it or create ampscript codeblocks / content blocks.

When writing your AMPscript, always remember that all calls will process at the end of the subscriber batch when performing email sends.

The above statement means that the ampscript will be executed when the email is inside the sending process (gets executed just before the email send process itself) on the server which then populates the content.

The script is therefore executed for each subscriber once (not once for all).

var @var
set @var = [dataextensioncolumnname]
IF @var = "something" THEN

Text number 1
Text number 2

or better ampscript that just uses variables that get populated differently. And then print out the variables at the end with commands like %%=v(@var)=%%

  • The rules are rather complex, but it boils down to sending a personalised value to each customer. I was wondering, is it possible to do something like this? "pre-populate values in a data extension, in each execution take a value from the data extension, print it and delete it so that for the next execution the value is different" Feb 13, 2019 at 13:12
  • These values that are unique for every customer that need to be displayed in the email, where are these located? Do you have a csv or other kind of file containing these values? If yes, then you can set up an automation and use import activity to update the DE with the latest values on a timely basis. And use Ampscript code in your email to lookup values from that DE. So you can schedule the automation in such a way that before every execution, the DE is updated with recent values.
    – Krati Garg
    Feb 13, 2019 at 14:20
  • Yes, I can assume that the values are in a file. But the users of my system need to trigger the import at will, not on a scheduled time. Can DEs be manipulated from AMPscript in the way I described above? "in each execution take a value from the data extension, print it and delete it so that for the next execution the value is different"" Feb 13, 2019 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.