0

hello currently working on an email that has table and the records are coming from another DE so im using LookupRows using contactId, I want to limit the number of records to be displayed to 5 however when i manually hardcode for @i = 1 to 5 Im getting an error because of those records who have lesser rows

ie

ContactId - Orders (no of rows)

I hope someone would guide me, Thank you

%%[ 
set @rows = LookupRows("Data extension name","ContactId", @contactId)
 
for @i = 1 5 do 

set @row = Row(@rows, @i) 

set @orderNumber = Field(@row, "Order Number") 
set @orderDate= Field(@row, "Order Date") ]%% 

   <tr>

   <td%%=v(@orderNumber)=%%</td>

<td%%=v(@orderDate)=%%</td>

</tr>

%%[next @i]%%
5

1 Answer 1

2

You'll need to check the rowCount of your lookup first.

If there are less than 5 rows it won't compile.

<table border="1">
<tr>
 <th>Order Number</th>
 <th>Order Date</th> 
</tr>

%%[ 

set @contactID = "123"
set @rows = LookupRows("iowPB5mD","ContactID", @contactID)
set @rowCount = rowCount(@rows)
set @rowCount = iif(@rowCount > 5, 5, @rowCount)
output(concat("<br>ContactID: ", @contactID))
output(concat("<br>rowCount: ", @rowCount))

]%%

%%[ for @i = 1 to @rowCount do ]%%

      %%[
         set @row = Row(@rows, @i) 
         set @orderNumber = Field(@row, "Order Number") 
         set @orderDate= Field(@row, "Order Date") 
      ]%% 

         <tr>
         <td>%%=v(@orderNumber)=%%</td>
         <td>%%=v(@orderDate)=%%</td>
         </tr>

%%[ next @i ]%%

</table>

Output:

https://mcsnippets.herokuapp.com/s/kkvA4v3k

You can test my code here: https://mcsnippets.herokuapp.com/s/kkvA4v3k

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.