I am currently aware of the following resources:
- AMPScript For Loop Dynamic Variable
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 ENDIF IF RowCount(@ExistingChild2) > 0 THEN FOR @j = 1 TO RowCount(@ExistingChild2) DO Set @Child2Row = Row(@ExistingChild2,@j) Set @Child2SubscriberKey = Field(@Child2Row, 'Id') NEXT @j ENDIF 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 ENDIF NEXT @i
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?
I have tested outputting a test variable which returns the below
testing = %%[SET @Child4SubscriberKey = Field(@ChildRow, Id)]%%