0

Is there a try/catch function in AMPscript? If not, how do you deal with errors in your code?

I have a code that may break when using lookUpRows, but i want it to do it again, but with another id. It breaks on 5th line

%%[

FOR @rowIterator = 1 TO 12 DO 

    set @productsRowCount = RowCount(@productRows)
    set @productRow = Row(@productRows, random(1, @productsRowCount))
    set @productId = v(field(@productRow, "product_id"))
    set @product = Row(lookUpRows("All Products", "product_id", @productId),1)

    IF v(field(@product, "state")) != "published" THEN 

        set @productRow = Row(@productRows, random(1, @productsRowCount))
        set @productId = v(field(@productRow, "product_id"))
        set @product = Row(lookUpRows("All Products", "product_id", @productId),1)
        IF v(field(@product, "state")) != "published" THEN 
            set @productRow = Row(@productRows, random(1, @productsRowCount))
            set @productId = v(field(@productRow, "product_id"))
            set @product = Row(lookUpRows("All Products", "product_id", @productId),1)
        ENDIF
        IF v(field(@product, "state")) != "published" THEN 
            set @productRow = Row(@productRows, random(1, @productsRowCount))
            set @productId = v(field(@productRow, "product_id"))
            set @product = Row(lookUpRows("All Products", "product_id", @productId),1)
        ENDIF
    ENDIF

    set @publishedProducts = Concat(@publishedProducts,@productId,"|")

NEXT @rowIterator

]%%
1
  • 1
    You don't need to use the v() function inside an AMPscript blocks. You can just reference the @variables by themselves. Jan 29, 2019 at 14:25

2 Answers 2

1

Since it is out of context a bit, I had to make some assumptions - but the two best ways I can think of to do it are by looking up based on 'State' equaling 'published' in your initial lookup, removing the possibility that you will ever get any non-published products. Or by using a do/while SSJS loop mixed with your AMPscript to ensure you always return 12 products.

Choice 1: (with an updated lookup statement)

set @publishedProductsRows = lookUpRows("All Products", "state", "published")
set @productsRowCount = RowCount(@publishedProductRows)

IF @productsRowCount > 0 THEN

    FOR @rowIterator = 1 TO 12 DO 

        set @productRow = Row(@publishedProductRows, random(1, @productsRowCount))
        set @productId = field(@productRow, "product_id")

        set @publishedProducts = Concat(@publishedProducts,@productId,"|")

        ENDIF

    NEXT @rowIterator

ENDIF


Choice 2: (combining AMPscript and SSJS do/while)

%%[
/* Your Previous Code */
set @productsRowCount = RowCount(@productRows)
]%%

<script runat="server">
var count = 0;

do {
</script>

%%[
    set @productRow = Row(@productRows, random(1, @productsRowCount))
    set @productId = v(field(@productRow, "product_id"))
    set @product = Row(lookUpRows("All Products", "product_id", @productId),1)

    IF field(@product, "state") != "published" THEN 

]%%

<script runat="server">
        var count += 1;
</script>

%%[
        set @publishedProducts = Concat(@publishedProducts,@productId,"|")

    ENDIF
]%%

<script runat="server">
} while (count < 12);
</script>
0

Instead of doing try- which is not available in AmpScript, why not create a condition on RowCount?

set @products = lookUpRows("All Products", "product_id", @productId)
IF RowCount(@products) = 0 THEN 
set @products = lookUpRows("All Products", "product_id", @productId2) 
ENDIF
set @product = Row(@products,1)

In this case, you get a second chance, where you can do a lookup with another value of Product ID, if your first attempt returns an empty result.

You must log in to answer this question.

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