0

Currently I'm trying to automate our coupon code process by making as many things dynamically loaded into the email. E.g. when someone has ordered a product multiple times, the exact amount of codes should be shown. This is being done by using a loop.

/* LOOP BASED ON FIELD aantal */
FOR @i = 1 TO @aantal DO 

   /* FOR EACH BASED ON FIELD aantal FROM DATA EXTENSION */
   IF @msgContext != "PREVIEW" THEN
   /* CREATE A UNIQUE COUPON ID BASED ON LOOP NUMBER (@i) AND ORDERLINE ID TO CLAIM MORE THEN 1 COUPON CODE*/
      SET @uniqueCouponID = CONCAT(@orderlineID, "-", @i)
      SET @coupon_de = CONCAT("codelijst_",@mdm)

      /* CLAIM A ROW FOR EACH IN LOOP */
      SET @CouponRow = ClaimRow(@coupon_de, "IsClaimed", "JobID", JobID, "ListID", ListID, "BatchID", _JobSubscriberBatchID, "SubscriberID", SubscriberID, "EmailAddress", @em, "UniqueCouponID", @uniqueCouponID, "OrderID", @orderlineID) 

      /* CHECK IF THERE IS A ROW */
      IF EMPTY(@CouponRow) THEN
      raiseerror("no more codes available", 1)
      ELSE
      SET @CouponCode = FIELD(@CouponRow,"CouponCode")
      ENDIF
   ELSE SET @CouponCode = "TEST"
ENDIF
]%% -->
Code: %%=v(@CouponCode)=%%<br>
<!-- %%[
NEXT @i
]%% -->

However to automate the process, we need to make a variable for the data extension. Unfortunately this isn't possible when using the claimrow function since it only takes the literal data extension name.

So I tried to recreate this process by using LookupOrderedRowsCS and UpdateDe. Things seemed to work fine when the @aantal = 1. However when we need to send multiple codes, only the first code is being claimed and shown as many times as defined by 'aantal'. How can I make sure that not the same code is being used?

<!-- %%[
/* LOOP BASED ON FIELD aantal */
FOR @i = 1 TO @aantal DO 
/* FOR EACH BASED ON FIELD aantal FROM DATA EXTENSION */

   IF @msgContext != "PREVIEW" THEN

      SET @uniqueCouponID = CONCAT(@orderlineID, "-", @i)
      SET @coupon_de = CONCAT("codelijst_",@mdm)
      SET @rsCoupon = LookupOrderedRowsCS(@coupon_de, 0, "CouponCode Asc", "isClaimed", 0)

      IF EMPTY(@rsCoupon) THEN
      raiseerror("Geen coupon codes meer beschikbaar", 1)
      ELSE
      SET @CouponCode = Field(Row(@rsCoupon, 1), "CouponCode")
UpdateDE(@coupon_de, 2, "CouponCode", @CouponCode, "IsClaimed", "False", "JobID", JobID, "ListID", ListID, "BatchID", _JobSubscriberBatchID, "SubscriberID", SubscriberID, "EmailAddress", @em, "UniqueCouponID", @uniqueCouponID, "OrderID", @orderlineID, "IsClaimed", "True")
      ENDIF
   ELSE SET @CouponCode = "TEST"
   ENDIF

]%% -->Code: %%=v(@CouponCode)=%%<br>
<!-- %%[
NEXT @i

1 Answer 1

0

You can work around static naming restrictions in functions with the TreatAsContent() function, which forces any AMPScript in a string to get evaluated.

%%[

var @cr
set @cr = ""
set @cr = concat(@cr, 'ClaimRow("', @coupon_de, '"')
set @cr = concat(@cr, ', "IsClaimed" ')
set @cr = concat(@cr, ', "JobID", ', JobID)
set @cr = concat(@cr, ', "ListID", ', ListID)
set @cr = concat(@cr, ', "BatchID", ', _JobSubscriberBatchID)
set @cr = concat(@cr, ', "SubscriberID", ',SubscriberID)
set @cr = concat(@cr, ', "EmailAddress", "', @em, '"')
set @cr = concat(@cr, ', "UniqueCouponID", "', @uniqueCouponID, '"')
set @cr = concat(@cr, ', "OrderID", "', @orderlineID, '"')
set @cr = concat(@cr, ')')

SET @CouponRow = TreatAsContent(@cr)

]%%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .