0

I'm attempting to fill out a table in an email with a DE contact's order info, using AMPscript/HTML. Am working in an HTML block. I have two sets that work in different circumstances but not all. Goal is to send one email, even if there are multiple orders per contact, and break each order into its own table labelled by order number.

Here's the first:

  %%[
var @rows, @row, @rowCount, @numRowsToReturn, @contactid, @i, @prevOrderNo

set @contactid = AttributeValue("Contact ID")
set @numRowsToReturn = 0 /* 0 means all */
set @rows = LookupOrderedRows("Order Follow Up", @numRowsToReturn, "Order Number", "Contact ID", @contactid)

set @rowCount = rowcount(@rows)

if @rowCount > 0 then

    set @prevOrderNo = ""

    for @i = 1 to @rowCount do

        var @OrderNo, @itemid, @price, @qty
        set @row = row(@rows,@i)

        set @OrderNo = field(@row,"Order Number")
        set @itemid = field(@row,"Item ID")
        set @price = field(@row,"Price")
        set @qty = field(@row,"Qty")

 if empty(@prevOrderNo) or @prevOrderNo != @OrderNo then 
           outputline(concat("<br>Order #", @OrderNo, "<br>"))
           outputline(concat('<table class="test"><tr>
               <td>QTY</td>
               <td>ITEM</td>
               <td>PRICE</td></tr>'))
           set @prevOrderNo = @OrderNo
        endif

          outputline(concat("<tr><td>", @qty, "</td>"))
          outputline(concat("<td>", @itemid, "</td>"))
          outputline(concat("<td>", @price, "</td></tr><br>"))
next @i
outputline(concat("</table>"))
else

    outputline(concat("<br>No transactionsList rows found"))

endif
]%%

The above works when the recipient has more than one item on an order, but when they have more than one order per email, it breaks down. The tables for both orders get misplaced below the second order number.

Below is the second I tried. Before making you scan it all, note the main difference is the placement of the closing </table> tag. I suspect this is the culprit but am not sure how else to fix it within constraints of the AMPscript, specifically the loop.

  %%[
var @rows, @row, @rowCount, @numRowsToReturn, @contactid, @i, @prevOrderNo

set @contactid = AttributeValue("Contact ID")
set @numRowsToReturn = 0 /* 0 means all */
set @rows = LookupOrderedRows("Order Follow Up", @numRowsToReturn, "Order Number", "Contact ID", @contactid)

set @rowCount = rowcount(@rows)

if @rowCount > 0 then

    set @prevOrderNo = ""

    for @i = 1 to @rowCount do

        var @OrderNo, @itemid, @price, @qty
        set @row = row(@rows,@i)

        set @OrderNo = field(@row,"Order Number")
        set @itemid = field(@row,"Item ID")
        set @price = field(@row,"Price")
        set @qty = field(@row,"Qty")

 if empty(@prevOrderNo) or @prevOrderNo != @OrderNo then 
           outputline(concat("<br>Order #", @OrderNo, "<br>"))
           outputline(concat('<table class="test"><tr>
               <td>QTY</td>
               <td>ITEM</td>
               <td>PRICE</td></tr>'))
           set @prevOrderNo = @OrderNo
        endif

          outputline(concat("<tr><td>", @qty, "</td>"))
          outputline(concat("<td>", @itemid, "</td>"))
          outputline(concat("<td>", @price, "</td></tr><br></table>"))
next @i

else

    outputline(concat("<br>No transactionsList rows found"))

endif
]%%

This one works in reverse: if there are multiple orders in an email, it's great. But if there are multiple items in one order, it fails to place subsequent line items in the same table as the first.

0

Call it a matter of personal tastes, but I would avoid outputline in favor of actual html with inline ampscript. at least to me, all sense of indentation is lost in your examples, and the code gets much harder to read. I think as a result, logical connections get blurred that a normally formatted HTML would visualize better.

So by restructuring the code in that manner, I think I found a reason why you do not see the intended behavior: your s and the different parts of the table are inside and outside of an IF condition, and that condition changes during your loops. So you might get stuck with unmatched pairs, e.g. an unclosed table. See below for my untested attempt. For each order with an order #, is always there. All content inside of the table(s) is shown if your prevOrder condition is met. Might not be 100% accurate but this is where I would start.

%%[

set @contactid = AttributeValue("Contact ID")
set @numRowsToReturn = 0 /* 0 means all */
set @rows = LookupOrderedRows("Order Follow Up", @numRowsToReturn, "Order Number", "Contact ID", @contactid)
set @rowCount = rowcount(@rows)

IF @rowCount > 0 THEN

  set @prevOrderNo = ""
  FOR @i = 1 to @rowCount do
    set @row = row(@rows,@i)
    set @OrderNo = field(@row,"Order Number")
    /* create a table for each order */
    
    If not empty(@orderNo) THEN
    ]%%
      <br>
      Order #%%=v(@OrderNo)=%%<br>
      <table class="test">
      %%[
      set @itemid = field(@row,"Item ID")
      set @price = field(@row,"Price")
      set @qty = field(@row,"Qty")
      IF empty(@prevOrderNo) 
      or @prevOrderNo != @OrderNo 
      THEN
        /* if condition for order display is met, show table content */
        ]%% 
        <tr>
         <td>QTY</td>
         <td>ITEM</td>
         <td>PRICE</td>
        </tr>
        <tr>
          <td>%%=v(@qty)=%%</td>
          <td>%%=v(@itemid)=%%</td>
          <td>%%=v(@price)=%%</td>
        </tr>
      %%[
        set @prevOrderNo = @OrderNo
      ENDIF
      ]%%
      /*close each table unconditionally*/
      </table><br>
      %%[
    ENDIF
  NEXT @i
ELSE
  ]%%
  <br>No transactionsList rows found
  %%[
ENDIF
]%%
2
  • This is easier to work with than my original for sure. Only edit I made is removing the ]%% just before the NEXT @i. It works like a charm when the contact has multiple order numbers per email but breaks down when the contact has multiple items per order. It puts the first item in the table, reiterates the order number underneath that, and leaves out subsequent items. Is it something in the loop logic? I'm at a loss. – lydiaj Oct 26 '20 at 14:06
  • removed the wrong bit, thanks. You might just need a second loop around the current one. Outer loop: for each order, create table. Inner loop: for each table, create tr and tds. – Jonas Lamberty Oct 26 '20 at 14:13
0

In the second example, your table is closing at the end of the first @i rotation, so none of the other @i's will display. Try putting the closing table tag outside of the ampscript loop after the next i. You will need to have this section of ampscript in the relevant HTML section of the page itself, not just in the header.

1
  • Thanks for your response. My first example has the table tag after the next @i as you say, but it misplaces the first @i rotation's table under a subsequent @i's order number label. I can't figure out what's causing this to happen. – lydiaj Oct 22 '20 at 20:31

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.