2

I have the following code to retrieve the series of a product that has been purchased. We are using a loop because a contact might purchase multiple products.

 %%[IF NOT EMPTY(@PersonAccountId) 
   THEN set @MyWatchesTable = RetrieveSalesforceObjects("My_Watches__c","Product__c","Person_Account__c", "=", @PersonAccountId,"Status__c","=", "Owner") 
   SET @rowCount = RowCount(@MyWatchesTable) 
   IF @rowCount > 0 THEN 
   FOR @counter = 1 to @rowCount DO 
   SET @MyWatchesRow = Row(@MyWatchesTable, @counter) 
   SET @Product = Field(@MyWatchesRow,"Product__c") 
SET @ProductTable = RetrieveSalesforceObjects("Product2","Series__c","Id", "=", @Product)
set @productCount = RowCount(@ProductTable)
set @ProductRow = Row(@ProductTable, @productCount) 
set @Series = Field(@ProductRow, "Series__c") 

]%%
-->
 Product %%=v(@counter)=%% : %%=v(@Product)=%%<br>
 Series %%=v(@productCount)=%% : %%=v(@Series)=%% <br>
 %%[ next @counter ]%% %%[ else ]%% No records found %%[ endif ]%%
<!-- %%[ENDIF]%%-->

I need to use the results of the loop to select the corresponding product checkbox if they own the product.

I wanted to concatenate the loop results

set @OutPutSeries = Concat(@counter, " ", @Product, " ", @productCount, " ", @Series, " ", next @counter)

and then use the the IndexOf to determine if the concatenated result contains one of the products

/* Does it match; ; if no match, output of IndexOf(1,2) will be "0" */ 
if IndexOf(@Series,'Connected') > 0 then 
    Set @OutputSoldConnected = "True"
else 
     Set @OutputSoldConnected = "False"
endif 

Then use the True/False result to pre-populate the checkbox

I have tried to use the indexOf on the @Series variable but it only returns the last result, that is why I thought about concatenating them but it did not work.

As @Mark G I had to set the variable before the loop @ProductSeriesOutput and declare the variable in the concatenation. Then I used the IndexOf function to check if the value I needed for the preference was available and selected the checkbox if matched.

 %%[IF NOT EMPTY(@PersonAccountId) 
   THEN 
   set @ProductSeriesOutput = " "
   set @MyWatchesTable = RetrieveSalesforceObjects("My_Watches__c","Product__c","Person_Account__c", "=", @PersonAccountId,"Status__c","=", "Owner") 
   SET @rowCount = RowCount(@MyWatchesTable) 
   IF @rowCount > 0 THEN 
   FOR @counter = 1 to @rowCount DO 
   SET @MyWatchesRow = Row(@MyWatchesTable, @counter) 
   SET @Product = Field(@MyWatchesRow,"Product__c") 
SET @ProductTable = RetrieveSalesforceObjects("Product2","Series__c","Id", "=", @Product)
set @productCount = RowCount(@ProductTable)
set @ProductRow = Row(@ProductTable, @productCount) 
set @Series = Field(@ProductRow, "Series__c") 
set @ProductSeriesOutput = Concat(@ProductSeriesOutput, @counter, @Series," ")
next @counter
else 
endif


IF NOT EMPTY(@Series)
THEN

SET @HasTagHeuer = "True"

Var @OutputSoldConnected

/* Does it match; ; if no match, output of IndexOf(1,2) will be "0" */ 
if IndexOf(@ProductSeriesOutput,'Connected') > 0 then 
    Set @OutputSoldConnected = "True"
else 
     Set @OutputSoldConnected = "False"
endif 


ENDIF
]%%
-->

<!-- %%[ENDIF]%%-->
7

If you need to append values to a variable from within a loop, you'll need to remember to include the variable itself during concatenation. Otherwise, you'll simply be "re-setting" the variable after each iteration (i.e., the variable will be set according to the last iteration, only).

For example, this yields only the last result:

%%[
for @counter = 1 to 3 do
  set @series = Concat(@counter, " ")
next @counter
]%%
%%=V(@series)=%%

Output:

3

This, on the other hand, will yield all concatenated values:

%%[
set @series = "" /* initialize outside of loop */

for @counter = 1 to 3 do
  set @series = Concat(@series, @counter, " ") /* include existing value first */
next @counter
]%%
%%=V(@series)=%%

Output:

1 2 3

  • I just tried what you suggested and although I set the variable I want to display before the loop it does not work. Do you mean I need to set the Series before the loop and then define it in the loop twice? Please note I have updated the question with the recent code – SYUYDY Aug 14 at 10:57
  • 1
    @SYUYDY You need to concatenate the same variable name to itself: set @ProductSeriesOutput = Concat(@ProductSeriesOutput,@counter," ",@Series," "). Afterwards, IndexOf() will be used on @ProductSeriesOutput (not @Series). Also, I count 2 if statements, but only 1 closing endif statement in your recent code, which means you're missing a final closing endif statement. – Mark G Aug 14 at 12:51
  • Thank you @Mark G that is clear now :) It worked – SYUYDY Aug 14 at 13:24

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.