0

I'm a real novice with AMPscript, and I have some that works for listing items on an order for one customer in one-to-many relationship. Now I have another challenge, and I need to add something to the loop logic. I just don't know where in the loop this piece goes.

I have data extension "Order_Data" with example data such as below:

Order Number   Customer Number   Item ID   Price   Qty   Web Enabled
1              101               Shirt     $5      1     Y
1              101               Shoes     $10     1     N
2              201               Pants     $7      1     N
3              201               Shirt     $5      1     Y
4              401               Shirt     $5      1     Y
4              401               Hat       $2      1     Y

Below is my AMPscript that summarizes their order in a really simplified table (sorry if the HTML is messy for reading, for some reason it makes most sense to me this way). So customer 101 receives 1 email with 2 items in the table. Customer 2 receives 2 emails, with one item in each email, etc. This all works fine so far.

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

set @OrderNo = AttributeValue("Order Number")
set @numRowsToReturn = 0 /* 0 means all */
set @rows = LookupOrderedRows("Order_Data", @numRowsToReturn, "Customer Number", "Order Number", @OrderNo)

set @rowCount = rowcount(@rows)

if @rowCount > 0 then

    set @prevOrderNo = ""

    for @i = 1 to @rowCount do

        var @customerno, @itemid, @price, @qty, @webenabled, @webofferblock
        set @row = row(@rows, @i) /* get row based on loop counter */

        set @customerno = field(@row,"Customer Number")
        set @itemid = field(@row,"Item ID")
        set @price = field(@row,"Price")
        set @qty = field(@row,"Qty")
        set @webenabled = field(@row,"Web Enabled")

      IF @webenabled == "Y" THEN
      SET @webofferblock = CONCAT("You can buy these items online! Save 10% when you use discount code EXAMPLE.")
      ENDIF
      
 if empty(@prevOrderNo) or @prevOrderNo != @OrderNo then 
           outputline(concat("Order # ", @OrderNo))
           outputline(concat("<table>
    <td>QTY</td>
    <td>ITEM</td>
    <td>UNIT PRICE</td></tr>"))
           set @prevOrderNo = @OrderNo
        endif

 outputline(concat("<tr><td>", @qty, "</td>"))
 outputline(concat("<td>", @itemid, "</td>"))
 outputline(concat("<td>$",@price, "</td></tr>"))


 
next @i
 outputline(concat("</table><br>", @webofferblock))

else

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

endif

]%%

Here's the piece I can't figure out. I want to check the value of all the Web Enabled rows per item id, and only if all are Y, display the webofferblock variable (but outside the table and only once). You can see I have an IF statement set up attempting that, but what it's actually doing (I think) is checking only if the last item id in the loop is Web Enabled == Y, and if so, it displays the message in webofferblock.

Is what I want possible? I was thinking something like "If the count of items in the loop with a Web Enabled value of Y == the count of total items in the loop, then display webofferblock." But I'm a newbie to the syntax and can't figure out the loop logic.

0

I'd suggest to switch to opening / closing ampscript and plain HTML instead of outputline - the structure does get lost a little.

Other than that - the only thing you are missing seems to be a simple reversal of your logic.

From:

"only if all are Y, display the webofferblock variable"

To:

"if one is NOT Y, hide the block."

If that's the case, define a new variable as a kind of "marker". I indicated with comments where I inserted that bit.

After the end of the for loop, simply make the display of your block conditional under that marker condition, see second set of comments.

var @rows, @row, @rowCount, @numRowsToReturn, @OrderNo, @i, @prevOrderNo

SET @OrderNo = AttributeValue("Order Number")
SET @numRowsToReturn = 0 /* 0 means all */
SET @rows = LookupOrderedRows("Order_Data", @numRowsToReturn, "Customer Number", "Order Number", @OrderNo)
SET @rowCount = rowcount(@rows)

IF @rowCount > 0 THEN

  SET @prevOrderNo = ""

  FOR @i = 1 to @rowCount DO

    var @customerno, @itemid, @price, @qty, @webenabled, @webofferblock
    SET @row = row(@rows, @i) /* get row based on loop counter */
    SET @customerno = field(@row,"Customer Number")
    SET @itemid = field(@row,"Item ID")
    SET @price = field(@row,"Price")
    SET @qty = field(@row,"Qty")
    SET @webenabled = field(@row,"Web Enabled")

    IF @webenabled == "Y" THEN
      SET @webofferblock = CONCAT("You can buy these items online! Save 10% when you use discount code EXAMPLE.")
    /* set a marker once at least one loop iteration comes across a "non web enabled" line. */      
       SET @hideBlock = true 

    ENDIF
   
    IF empty(@prevOrderNo) 
    or @prevOrderNo != @OrderNo 
    THEN
      ]%%
      Order # %%=v(@OrderNo)=%%
      <table>
        <tr>
          <td>QTY</td>
          <td>ITEM</td>
          <td>UNIT PRICE</td>
        </tr>
      </table>
      %%[
      SET @prevOrderNo = @OrderNo
    ENDIF 
    ]%%
    <table>
      <tr>
        <td>%%=v(@qty)=%%</td>
        <td>%%=v(@itemid)=%%</td>
        <td>$%%=v(@price)=%%</td>
      </tr>
    </table>
    %%[ 
  NEXT @i
  /* new if check: use the marker to determine show / hide */
  IF @hideBlock != true THEN
    ]%%
    <br>
    %%=v(@webofferblock)=%%
    %%[
  ENDIF
  /* end new if check */
ELSE
  ]%%
  <br>
  No transactionsList rows found
  %%[
ENDIF ]%%
4
  • Thanks for your reply! I've been testing yours out. It still wants to display the webofferblock when the last item in the loop is web enabled == Y, even if the rest are N. It's almost like the showBlock is getting set from true to false back and forth each i, until it ultimately stops at whatever the last item's web enabled value is. I just can't figure out how to work around this.
    – lydiaj
    May 17 at 21:30
  • edited my answer from showblock to hideblock. :D The problem should have been that "showblock" was in an ELSE condition, which is indeed called every iteration and resetting, as you rightly say! Now it should work, pls check again. May 18 at 6:39
  • OK, I tried this. It works now to hide the webofferblock when not all the items are Y, but now it doesn't display when all items == Y. I stumbled into a workaround while playing around, so I'll mention in case it helps someone else. I SET webofferblock outside the IF statement it was in but kept the hideblock inside. Then in the new IF check below, I changed the operator from != to == true. Now it shows the webofferblock when there is at least one Y item. I made an IF stmnt to add a * to Y items, to reference the block. If you have more thoughts on original use case, I'm still working on it!
    – lydiaj
    May 18 at 14:15
  • I might have it! I kept the SET webofferblock outside the conditional, as in my previous comment, and I edited the conditional: IF @webenabled == "N" THEN SET @hideBlock = true ENDIF. Then I kept what you wrote for the second new IF check: IF @hideBlock != true THEN ]%% <br> %%=v(@webofferblock)=%% %%[ ENDIF . . .etc. So far, that's working in every test. Many thanks for your help!
    – lydiaj
    May 18 at 14:45

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.