0

I am trying to loop through a number of rows, and return only rows with a specific date criteria (customers who has unsubscribed within 200 days). If that criteria is not met, i want to raise an error. I cant get it to raise the error when no rows is found and when the date criterie is not met. Can anyone point me in the right direction? It will be greatly appreciated.

This is the script so far:

<!-- AMPscript start --> 

%%[
/* declare vars */

var @UnsubscribeCustomer, @UnsubscribeData, @butik, @StoreName, @mail, @antal, @x, @UnsubscribeDate, @rows, @Rows, @Count, @date, @diff

SET @butik = StoreId
SET @Rows = LookupRows('Customers_web','StoreId', @butik)
SET @Count = Rowcount(@rows)

IF @Count > 0 THEN
for @x = 1 to @Count do

    SET @Row = Row(@Rows,@x)
    SET @mail = Field(@Row,21) <br>
    SET @date = Field(@Row,35)

IF @date > DateAdd(Now(1), '-200', 'D') THEN

]%%
<span style="font-family:arial,helvetica,sans-serif;"><b>Email address on Customer:</b> <br>%%=v(@mail)=%% <br><br> <b>Date for unsubscription</b>: <br> %%=v(@date)=%%</span><br><br><hr>
%%[

SET @Count = @x

ENDIF

next @x
ELSE
RaiseError('There is no unsubscriptions for this store')
EndIF
  ]%%

<!-- AMPscript End-->

Thank you in advance.

0

First of all, you don't have to declare your variable with var. Also, for your Field functions, I think it is better to write the field name like

SET @mail = Field(@Row,"Email_address")

Hence, your final code might be something like that:

%%[

SET @butik = StoreId

SET @Rows = LookupRows('Customers_web','StoreId', @butik)

SET @Count = Rowcount(@rows)

IF @Count > 0 THEN for @x = 1 to @Count do

SET @Row = Row(@Rows,@x)
SET @mail = Field(@Row,"Email_address") 
SET @date = Field(@Row,"Date")

IF @date > DateAdd(Now(1), '-200', 'D') THEN

]%%

Email address on Customer: 
%%=v(@mail)=%% 

Date for unsubscription: 
%%=v(@date)=%%


%%[
SET @Count = @x

ENDIF

next @x 

ELSE RaiseError('There is no unsubscriptions for this store') 

EndIF 


]%%
3
  • Hi Selim Thank you for response. I cant get the ELSE to return the RaiseError for the subscribers who do not meet criteria. I was thinking to use something like this: If @Count = 0 THEN RaiseError('Error Message') ENDIF I cant seem to get to work though.
    – M-vad
    Aug 3 '18 at 8:19
  • In my code, it is now the ELSE of "IF @Count > 0 ". Aug 3 '18 at 8:37
  • 2
    While you are right that AMPscript doesn't enforce you to declare variables, it is best practice to do so, as it will add the variables to an internal dictionary, which can provide performance benefits. Refer to documentation which explains this further. Aug 3 '18 at 11:04
0

I believe the stuff is wrong because you used ENDIF wrong. I am not completely sure if I understood that use case correctly, but for me, it was hard to understand which part come inside which if therefore I did some tabs and altered the logic a bit. That would be my guess:

%%[

var @UnsubscribeCustomer, @UnsubscribeData, @butik, @StoreName, @mail, @antal, @x, @UnsubscribeDate, @rows, @Rows, @Count, @date, @diff

SET @butik = StoreId
SET @Rows = LookupRows('Customers_web','StoreId', @butik)
SET @Count = Rowcount(@rows)

IF @Count == 0 THEN
    RaiseError('There is no unsubscriptions for this store') 
ELSEIF @Count > 0 THEN
    for @x = 1 to @Count do
        SET @Row = Row(@Rows,@x)
        SET @mail = Field(@Row,"Email_address") 
        SET @date = Field(@Row,"Date")
        IF @date > DateAdd(Now(1), '-200', 'D') THEN
]%%

Email address on Customer: 
%%=v(@mail)=%% 
Date for unsubscription: 
%%=v(@date)=%%

%%[
            SET @Count = @x
        ELSE 
            RaiseError('There is no unsubscriptions for this store') 
        ENDIF
    next @x 
ENDIF
]%%

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.