[SalesForce] Send a coupon – Claim row

I am using the following code in order to retrieve a coupon from a Data Extension, but I have an issue.
When I sent an email to several contacts, they received the same coupon.
Could you tell why this code doesn't work correctly?

set @rows = LookupRows("CouponCodes","Country", @countrycode, "IsClaimed", "False")

if rowcount(@rows) > 0 then
  set @row = Row(@rows,1) 
  set @couponCode = field(@row,"Code")
else

if _messagecontext == "PREVIEW" then
       set @couponCode = "XX TEST XX"
else

  set @couponRow = ClaimRow("CouponCodes","IsClaimed","Contact_ID", @Contact_ID) 

  if empty(@couponRow) then
     /* You can do other error handling here if you want.*/
     /* This aborts the send */
     raiseError("no more coupons available")
  else 
     set @couponCode = field(@couponRow,"Code") 
     UpdateDE("CouponCodes",1,"Code", @couponCode,"Contact_ID", @Contact_ID)
  endif

endif 

endif

Best Answer

With this:

set @row = Row(@rows,1) 

You are selecting the first row if there is any coupon code for that country.

I think you need a for loop, something like that:

if rowcount(@rows) > 0 then

 FOR @i = 1 TO rowcount(@rows) DO   
    set @row = Row(@rows,@i)
    set @couponCode = field(@row,"Code")

You can use different coupon codes like that.

Related Topic