I am trying to implement unique coupon codes in SFMC.
I have created a data extension - Unique_Coupon_Codes like this,
This is the code I have written
%%[
if _messagecontext == "PREVIEW" then
set @couponCode = "TEST"
else
set @email=AttributeValue('EmailAddress')
<h1>email: %%=v(@email)=%%</h1>
set @IsClaimedValue = Lookup("Unique_Coupon_Codes","IsClaimed",'EmailAddress', @email)
if @IsClaimedValue==true then
set @CouponCode= Lookup("Unique_Coupon_Codes","CouponCode",'EmailAddress', @email)
else
set @CouponDE = "Unique_Coupon_Codes"
TreatAsContent(Concat("%","%[ SET @couponRow = ClaimRow('", @CouponDE, "', 'IsClaimed','ClaimDate',Now(1), 'EmailAddress', '", @email, "') ]%","%"))
") ]%","%"))
if not empty(@couponRow) then
set @CouponCode = Field(@couponRow,"CouponCode")
else
RaiseError("No coupons available", true)
endif
endif
endif
]%%
CouponCode: %%=v(@CouponCode)=%%
What is expected: When I send the email for the first time to a customer the code should pick up a new coupon code and assign it to that customer. Next time when same customer enters the journey instead of claiming a new row, the code should fetch the values from the row that was claimed earlier and display that, instead of claiming a new row.
My problem: When the email is sent for first time, the rows are claimed. When the email is sent for the second time to same customer a new row is claimed (This should not happen) I am able to display the old claimed code to the customer but at same time a new rows gets claimed.
Can anyone help me fix this?