I am currently aware of the following resources:

  1. AMPScript For Loop Dynamic Variable
  2. https://gortonington.com/dynamically-creating-variables-in-ampscript/

I have a repeating code block that I want to make DRY using a for loop. The current code is:

IF RowCount(@ExistingChild1) > 0 THEN
  FOR @j = 1 TO RowCount(@ExistingChild1) DO
    Set @Child1Row              = Row(@ExistingChild1,@j)
    Set @Child1SubscriberKey    = Field(@Child1Row, 'Id')
  NEXT @j
IF RowCount(@ExistingChild2) > 0 THEN
  FOR @j = 1 TO RowCount(@ExistingChild2) DO
    Set @Child2Row              = Row(@ExistingChild2,@j)
    Set @Child2SubscriberKey    = Field(@Child2Row, 'Id')
  NEXT @j
IF... /*Repeat as necessary*/

I have tried to create a for loop to improve my code...

FOR @i = TO 4 DO
  Set @currentChild = Concat('ExistingChild', @i)
  IF RowCount(@currentChild) > 0 THEN
    FOR @j = 1 TO RowCount(@currentChild) DO
      Set @Child1Row  = Row(@currentChild,@j)
      Set @fieldSet   = TreatAsContent(CONCAT('%','%[SET @Child', @i, 'SubscriberKey = Field(@Child', @i, "Row, 'Id')])%", '%'))
    NEXT @j

But this currently doesn't work. It throws the error

Object reference not set to an instance of an object

Can anyone advise on what I am doing wrong?

Many thanks.


I have tested outputting a test variable which returns the below

testing = %%[SET @Child4SubscriberKey = Field(@ChildRow, Id)]%%
  • @currentChild's a String (eg: "ExistingChild1"), not a RowSet, so you'd not be able to loop around it. You may find SSJS a little easier to work with, here.
    – Macca
    Commented Dec 21, 2020 at 21:43
  • Hi @Macca, thanks for your answer/help. I'm pretty good with 'vanilla' JS and tried writing a for loop with template literals but it doesn't seem to work. Do you know of any resources that may help me? The actual docs don't seem to be very good. Commented Dec 22, 2020 at 11:15
  • Hi Liam. That error is occurring because of a null reference in the C# layer, and the only thing I can see that could do that is the "field" call. Here is the next step I would recommend: Put everything from the CONCAT call after TreatAsContent into a string variable and see what you get. When I have done things like this, I have often had to escape the special characters to get it to work, and you might have to do something as well. Report back on what that string is.
    – Timothy
    Commented Dec 22, 2020 at 22:38
  • Hi @Timothy - as suggested, I created a test variable to see what was being set. I have included it in an update at the bottom of the original post. Commented Dec 23, 2020 at 9:43

1 Answer 1


I believe the error is here:

Set @currentChild = Concat('ExistingChild', @i)

CONCAT will always return a string. The code immediately following:

IF RowCount(@currentChild) > 0 THEN

RowCount takes a row set as an argument, not a string. So this will blow up.

You will need to do some kind of LookupRows call on a DE first.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .