1

I understand For loops, but I'm struggling trying to set n SET variables.

Basically, what I'm trying to do is for each @i in a rowset, I'm trying to:

SET @SegLocator (and concatenate @i to the end) for each one I find, so that I can compare @SegLocator1 to @SegLocator2.

I'm good on looping, and concatenating, I'm just not clear on how to increment a new SET @SegLocator. I know the script below isn't valid, but it doesn't appear that I can do something like:

        %%[if not empty(FlightSegmentInfo) then 

          for @i = 1 to Rowcount(@rowsetSegLocator) do
          SET @SegLocator(@i) = Field(Row(@rowsetSegLocator,@i),1)
          ]%%
          %%=v(@SegLocator(@i))=%%<br/>

          %%[Next @i]%%

        %%[endif]%%

Is there a way to dynamically create a NEW set var based on an unknown number of rows?

I've also tried variants of the following:

        %%[if not empty(FlightSegmentInfo) then 

          for @i = 1 to Rowcount(@rowsetSegLocator) do
          SET (concat(@SegLocator,@i)) = Field(Row(@rowsetSegLocator,@i),1)
          ]%%
          %%=v(concat(@SegLocator,@i))=%%<br/>
          %%[Next @i]%%

...and...

        %%[if not empty(FlightSegmentInfo) then 

          for @i = 1 to Rowcount(@rowsetSegLocator) do
          SET (concat("@SegLocator",@i)) = Field(Row(@rowsetSegLocator,@i),1)
          ]%%
          %%=v(concat("@SegLocator",@i))=%%<br/>
          %%[Next @i]%%

...and finally...

        %%[if not empty(FlightSegmentInfo) then 

          for @i = 1 to Rowcount(@rowsetSegLocator) do
          SET @(concat("SegLocator",@i)) = Field(Row(@rowsetSegLocator,@i),1)
          ]%%
          %%=v(@(concat("SegLocator",@i)))=%%<br/>
          %%[Next @i]%%
2
  • this can be done,with a little trick found in the answer here: salesforce.stackexchange.com/questions/31819/… Commented Jul 23, 2019 at 18:40
  • Hi @JonasLamberty - Thanks for looking into this. Though, the answer states: "The following code requires that the variable names are the same except for an incrementing integer value, and also that you know the number of variables that exist ahead of time." However, it's the "unknown" for me that is the problem. Commented Jul 23, 2019 at 19:26

2 Answers 2

0

It doesn't appear that there is a way to do this. So, I addressed it by first creating up to 10 @SegLocatorX variables like this:

                              %%[if not empty(FlightSegmentInfo) then 

                              SET @rlsCount = Rowcount(@rowsetSegLocator)
                              for @i = 1 to Rowcount(@rowsetSegLocator) do

                              if @i == 1 then
                              SET @SegLocator1 = Field(Row(@rowsetSegLocator,@i),1)
                              SET @Airline1 = Field(Row(@rowsetAirline,@i),1)
                              SET @DepAirportCode1 = Field(Row(@rowsetDepAirportCode,@i),1)
                              SET @ArrAirportCode1 = Field(Row(@rowsetArrAirportCode,@i),1)
                              elseif @i == 2 then
                              SET @SegLocator2 = Field(Row(@rowsetSegLocator,@i),1)
                              SET @Airline2 = Field(Row(@rowsetAirline,@i),1)
                              SET @DepAirportCode2 = Field(Row(@rowsetDepAirportCode,@i),1)
                              SET @ArrAirportCode2 = Field(Row(@rowsetArrAirportCode,@i),1)
                              elseif @i == 3 then
                              SET @SegLocator3 = Field(Row(@rowsetSegLocator,@i),1)
                              SET @Airline3 = Field(Row(@rowsetAirline,@i),1)
                              SET @DepAirportCode3 = Field(Row(@rowsetDepAirportCode,@i),1)
                              SET @ArrAirportCode3 = Field(Row(@rowsetArrAirportCode,@i),1)
                              elseif @i == 4 then
                              SET @SegLocator4 = Field(Row(@rowsetSegLocator,@i),1)
                              SET @Airline4 = Field(Row(@rowsetAirline,@i),1)
                              SET @DepAirportCode4 = Field(Row(@rowsetDepAirportCode,@i),1)
                              SET @ArrAirportCode4 = Field(Row(@rowsetArrAirportCode,@i),1)
                              elseif @i == 5 then
                              SET @SegLocator5 = Field(Row(@rowsetSegLocator,@i),1)
                              SET @Airline5 = Field(Row(@rowsetAirline,@i),1)
                              SET @DepAirportCode5 = Field(Row(@rowsetDepAirportCode,@i),1)
                              SET @ArrAirportCode5 = Field(Row(@rowsetArrAirportCode,@i),1)
                              elseif @i == 6 then
                              SET @SegLocator6 = Field(Row(@rowsetSegLocator,@i),1)
                              SET @Airline6 = Field(Row(@rowsetAirline,@i),1)
                              SET @DepAirportCode6 = Field(Row(@rowsetDepAirportCode,@i),1)
                              SET @ArrAirportCode6 = Field(Row(@rowsetArrAirportCode,@i),1)
                              elseif @i == 7 then
                              SET @SegLocator7 = Field(Row(@rowsetSegLocator,@i),1)
                              SET @Airline7 = Field(Row(@rowsetAirline,@i),1)
                              SET @DepAirportCode7 = Field(Row(@rowsetDepAirportCode,@i),1)
                              SET @ArrAirportCode7 = Field(Row(@rowsetArrAirportCode,@i),1)
                              elseif @i == 8 then
                              SET @SegLocator8 = Field(Row(@rowsetSegLocator,@i),1)
                              SET @Airline8 = Field(Row(@rowsetAirline,@i),1)
                              SET @DepAirportCode8 = Field(Row(@rowsetDepAirportCode,@i),1)
                              SET @ArrAirportCode8 = Field(Row(@rowsetArrAirportCode,@i),1)
                              elseif @i == 9 then
                              SET @SegLocator9 = Field(Row(@rowsetSegLocator,@i),1)
                              SET @Airline9 = Field(Row(@rowsetAirline,@i),1)
                              SET @DepAirportCode9 = Field(Row(@rowsetDepAirportCode,@i),1)
                              SET @ArrAirportCode9 = Field(Row(@rowsetArrAirportCode,@i),1)
                              elseif @i == 10 then
                              SET @SegLocator10 = Field(Row(@rowsetSegLocator,@i),1)
                              SET @Airline10 = Field(Row(@rowsetAirline,@i),1)
                              SET @DepAirportCode10 = Field(Row(@rowsetDepAirportCode,@i),1)
                              SET @ArrAirportCode10 = Field(Row(@rowsetArrAirportCode,@i),1)

                              endif]%%

                              %%[Next @i]%% %%[endif]%% 
0

Given your answer, I am assuming all your rowsets have the same length. If I understand correctly, then the following should shorten your code and work with any number of lines in the rowset.

the idea is to have a TreatAsContent wrapper and broken up AMPScript tags. In this construct, concatenate an additional "@" before you want to use a variable inside your loop. Inside your current if-statement, you'd just have to apply that for 3 variables per line.

Which creates a long, clumsy concat :)

untested, but tried my best here -

%%[if not empty(FlightSegmentInfo) then 

    SET @rlsCount = Rowcount(@rowsetSegLocator)
    for @i = 1 to @rlsCount do
]%%
        %%=TreatAsContent( Concat( "%", "%[ SET @", @SegLocator, @i, " = ", "Field(Row(@",@rowsetSegLocator,",@",@i,"),1) ]%", "%") )=%%
        %%=TreatAsContent( Concat( "%", "%[ SET @", @Airline, @i," = ", "Field(Row(@",@rowsetAirline,",@",@i,"),1) ]%", "%") )=%%
        %%=TreatAsContent( Concat( "%", "%[ SET @", @DepAirportCode, @i," = ", "Field(Row(@",@rowsetDepAirportCode,",@",@i,"),1) ]%", "%") )=%%
        %%=TreatAsContent( Concat( "%", "%[ SET @", @ArrAirportCode, @i," = ", "Field(Row(@",@rowsetArrAirportCode,",@",@i,"),1) ]%", "%") )=%%
%%[
    Next @i
endif ]%%

---edit: part 2----

If you want to have a bit longer, but more readable code, maybe this is for you:

In general - If you have a big and everchanging number of Rowsets, consider retrieving the full (dynamic) number for your current problem, then storing them in a data extension in this fashion:

I am assuming here that "currentDate" is enough to mark the unique run. You can also use any matching unique identifier that ties your data to your loop.

type // key // value
myFlightData_[currentDate],SegLocator1,'a value'
myFlightData_[currentDate],Airline1,'a value'
myFlightData_[currentDate],DepAirportCode1,'a value'
myFlightData_[currentDate],ArrAirportCode1,'a value'
myFlightData_[currentDate],SegLocator2,'a value'
...
    %%[

 SET @keyValueDE = "ENT.myBigDE"
 SET @type = concat("myFlightData", now()) 
/*note: this won't work as now() is likely improperly formatted, but you get the idea*/

 SET @rowsetRetrieved = LookupRows(@keyValueDE, "type", @type)
 SET @rowCountRetrieved = RowCount(@rowsetRetrieved)

 IF (@rowCountRetrieved == 0 ) THEN
    SET @type = "default"
    SET @rowsetRetrieved = LookupRows(@keyValueDE, "type", @type)
    SET @rowCountRetrieved = RowCount(@rowsetRetrieved)
 ENDIF

 /* dynamic variable and assignment based on the key Value Data Extension Rows */

 FOR @i = 1 TO @rowCountRetrieved DO

   SET @row = row(@rowsetRetrieved,@i) /* get row based on loop counter */
   SET @createdVariable = field(@row,"Key")
   SET @createdVariableValue = field(@row,"Value")
]%%

%%=TreatAsContent(Concat("%","%[SET @",@createdVariable," = '",@createdVariableValue,"']%","%"))=%%

  %%[

 NEXT @i
 ]%%
1
  • edited my comment to put TreatAsContent in inline AMPScript Commented Jul 24, 2019 at 6:43

You must log in to answer this question.

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