I have problem with IF/ELSE statement in Ampscript ,as it is not going through else statement

I have created a DE for couponcode Claim coupon DE details(CouponCodeRedeem) CouponCode - CouponCode EmailAddress - Email to which coupon is sent IsClaimed - is set to Y once emai; with is sent to customer IsRedeemed - updated based on external system response ; used to send reminder email if coupon is not redeemed.

Receipient DE details EmailAddress - email address of the recipient Name - name of recipient

i have populated "coupon code for 3 rows in my DE , where IsRedeemed is set to Y for 1 coupon code and remaining 2 rows have value has false .

I'm trying to send coupon to customers for which new customers( IsRedeemed ="False") , but it is not going thorugh my else statement and check my if statement and sends the coupon for (Isredeemed = True) for 1 recipient .

enter image description here
Code Syntax :

var @email,@row,@couponRow, @couponCode,@ClaimedDate,@IsRedeemed,@barcode

set @rows = LookupRows("CouponCodeRedeem","IsRedeemed","Y")
set @rowCount = rowcount(@rows)

if @rowCount > 0 then
output(concat("rowcount: ", @rowcount))
  for @i = 1 to @rowCount do

    var @emailAddress, @couponCode
    set @row = row(@rows, @i) /* get row based on counter */
    set @emailAddress = field(@row,"EmailAddress")
    set @couponCode = field(@row,"CouponCode")

    <br>Row %%=v(@i)=%%, couponCode: %%=v(@couponCode)=%%, emailAddress: %%=v(@emailAddress)=%%


  next @i ]%%

%%[ else ]%%/*setting coupon against new customers*/

set @email = AttributeValue("emailAddr")
output(concat("email: ", @email))
set @couponRow = ClaimRow("CouponCodeRedeem", "IsClaimed", "EmailAddress", @email)

if not empty(@couponRow) then

  set @couponCode = Field(@couponRow, "CouponCode")
set @barcode = BarcodeURL(@couponCode, "Code128Auto", 300, 100)
set @ClaimedDate = Field(@couponRow, "ClaimedDate")

  RaiseError("No coupons available")

%%[ endif ]%%

the problem is your lookup statement. combined with the if - statement

set @rows = LookupRows("CouponCodeRedeem","IsRedeemed","Y")
set @rowCount = rowcount(@rows)

if @rowCount > 0 then
  //do something

You only make a count of columns where IsRedeemed == true and then iterate over these results. Your code will only reach the part of the else, when no results where found.

  • Hi Daniel , but i have only one codition to check and split the rows based on IsRedeemed value . Should i need to add AND condition in initial if block like if( rowcount > 0 AND IsRedeemed = "Y") . if( rowcount > 0){ do something } else { if(@copuon row not empty){ do something } else { throw error } } Jun 24 '20 at 13:19
  • Hi, I think you should do something like this: set @rows = LookupRows("CouponCodeRedeem","IsClaimed","False") then you can iterate the rows and for each one you can decide what to do dependent to the value of IsRedeemed Jun 25 '20 at 6:15

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.