4

I'm very comfortable with using for loops in AMPscript. However, someone recently mentioned that other loop contexts are supported, similar to JavaScript, which offers:

  • for - loops through a block of code a number of times
  • for/in - loops through the properties of an object
  • while - loops through a block of code while a specified condition is true
  • do/while - also loops through a block of code while a specified condition is true

I'm not sure which of the above is supported in AMPscript. I can't find any documentation or example code to indicate this extended support. Before I validate this for myself, has anyone tried this?

  • I have found no support for do/while or while (after a lot of digging). I can't say I have a definitive 'no', but I would say I am 99% certain. I just tried For/IN and got the syntax error that it 'requres TO or DOWNTO'. – Gortonington Jul 18 '17 at 20:03
  • You can use a SSJS try/catch to trap AMPScript errors. That might yield some extra details in the experiment. – Adam Spriggs Jul 18 '17 at 20:49
3

I have put some time into this and in the resources it only lists FOR/TO(DOWNTO) as a viable looping option.

I have attempted FOR/IN and received a definitive syntax error that FOR requires to be followed by TO or DOWNTO - which means the functionality is definitely not there.

I then investigated using WHILE, which has no documentation at all and provided no errors when used - BUT it did not actually do what it was supposed to do. After looking through the code and using AMPScript editors to check syntax, etc. I have found out that both DO and WHILE are not recognized as commands. This means that there is no inherit capability to achieve these.

Basically what it comes down to is that the only available inherit looping function in AMPScript is FOR/TO(DOWNTO) and without tremendous 'hacking' of the inherit functionality (not even sure THAT is possible), of which I have not yet found a good use case, it is not possible to use the other loop types directly in AMPScript. You would need to use SSJS to achieve these.

SFMC support (helpful as always) directed me to the documentation resource listed above and told me those are the only available commands/functions.

| improve this answer | |
5

My hacky stupid workaround for While loops in AMPscript:

%%[ FOR @i=1 to 2 DO
      /*Execute your code here*/
      IF NOT @condition THEN
        @i = Subtract(@i, 1)
      ENDIF
    NEXT @i ]%%

This loop will keep executing until the condition is met.

| improve this answer | |
  • Nice workaround! – Eliot Harper Jul 20 '17 at 20:22
  • Be careful- I have been told that the for loop variable is 'locked' and cannot be edited. I haven't tested it, but if this is the case, it can cause inconsistencies in your workaround. – Gortonington Jul 20 '17 at 23:18
  • 1
    @ncv - I attempted your code (honestly in hopes I was wrong, because that would help solve so much) but I get the following syntax error: The variable @i is in use by a processing loop and cannot be declared or set. Owning process: FOR LOOP 1. It appears this is not a viable workaround :( – Gortonington Jul 21 '17 at 12:23
  • 2
    You can set your own index as a workound to this workaround. just set an @index variable and increment that – garek007 Aug 30 '18 at 18:42
2

Based on ncv solution:

%%[ 
        SET @x = 2
        FOR @i=1 to @x DO
           /*Execute your code here*/
           IF NOT @condition THEN
             @x = add(@x, 1)
           ENDIF
        NEXT @i 
]%%
| improve this answer | |

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.