0

Is there a break function in AMPScript? I have following code snippet and would like to end the 'For' loop if the condition is met. Currently the code will transverse the whole DE.

How can I fix this?

%%[ set @DELookup = lookuprows('DE', 'SubscriberKey', SubscriberKey) 
if rowcount(@DELookup) >= 1 then
for @i = 1 to rowcount(@DELookup) do
set @instanceRow = row(@DELookup, @i)
set @ID = field(@instanceRow, 'columnID')
if not empty(@ID)then
// DO something
]%%
%%[else]%% %%[endif]%%
%%[next @i]%% %%[else]%% %%[endif]%%
]%%
1
  • This outputs something? The AMPScript block tags are not matching. – Adam Spriggs Aug 28 '17 at 11:20
1

Here is a very simple trick to brack the For Loop -

%%[ 
SET @Data = LookUpRows('DE','SubscriberKey',@SubscriberKey)  
SET @Count = RowCount(@Data)

IF @Count > 0 Then
    For @i = 1 To @Count Do
        SET @ID = Field(Row(@Data,@i),'columnID')
            IF Not Empty(@ID) Then
                Do Somthing....
                SET @Count = @i
            EndIF
    Next @i
EndIF
]%%
1
  • Please take some time to review How to Answer and add some context , link references as well as an explanation as to why your answer works. Thanks – glls Oct 20 '17 at 15:01
0

Try with this. If the condition is met, it will stop the for-loop.

%%[ 
set @DELookup = lookuprows('DE', 'SubscriberKey', @SubscriberKey)

if rowcount(@DELookup) >= 1 then

    for @i = 1 to rowcount(@DELookup) do

        set @instanceRow = row(@DELookup, @i)
        set @ID = field(@instanceRow, 'columnID')

        if not empty(@ID)then
            /* DO something */
        endif

    next @i

endif
]%%
1
  • Hi Adam, If I have 2 or more rows that met the condition, only the second row get executed. I would like to exit from the loop once condition is met so that 1st row will be executed and 2nd row later. – kLo Sep 13 '17 at 9:00

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.